`
duoerbasilu
  • 浏览: 1482733 次
文章分类
社区版块
存档分类
最新评论

自连接 in和exits比较

 
阅读更多

先看我从网上找的一段话:

*************************************开始线***********************************************

有两个简单例子,以说明 “exists”和“in”的效率问题

1) select * from T1 where exists(select 1 from T2 whereT1.a=T2.a) ;

<wbr><wbr><wbr>T1数据量小而T2数据量非常大时,T1&lt;&lt;T2时,1) 的查询效率高。</wbr></wbr></wbr>

2) select * from T1 where T1.a in (select T2.a from T2) ;

<wbr><wbr><wbr><wbr>T1数据量非常大而T2数据量小时,T1&gt;&gt;T2时,2) 的查询效率高。</wbr></wbr></wbr></wbr>

exists 用法:

其中 “select 1 from T2 where T1.a=T2.a” 相当于一个关联表查询,相当于

“select 1 from T1,T2 <wbr><wbr><wbr><wbr>whereT1.a=T2.a”</wbr></wbr></wbr></wbr>

但是,如果你当当执行 1) 句括号里的语句,是会报语法错误的,这也是使用exists需要注意的地方。

“exists(xxx)”就表示括号里的语句能不能查出记录,它要查的记录是否存在。

因此“select 1”这里的“1”其实是无关紧要的,换成“*”也没问题,它只在乎括号里的数据能不能查找出来,是否存在这样的记录,如果存在,这 1) 句的where条件成立。

<wbr></wbr>

in 的用法:

继续引用上面的例子

“2) select * from T1 where T1.a in (select T2.a from T2) ”

这里的“in”后面括号里的语句搜索出来的字段的内容一定要相对应,一般来说,T1和T2这两个表的a字段表达的意义应该是一样的,否则这样查没什么意义。

打个比方:T1,T2表都有一个字段,表示工单号,但是T1表示工单号的字段名叫“ticketid”,T2则为“id”,但是其表达的意义是一样的,而且数据格式也是一样的。这时,用2)的写法就可以这样:

“select * from T1 where T1.ticketid in (select T2.id from T2)”

Select name from employee where name not in (select name fromstudent);

Select name from employee where not exists (select name fromstudent);

第一句SQL语句的执行效率不如第二句。

通过使用EXISTS,Oracle会首先检查主查询,然后运行子查询直到它找到第一个匹配项,这就节省了时间。Oracle在执行IN子查询时,首先执行子查询,并将获得的结果列表存放在一个加了索引的临时表中。在执行子查询之前,系统先将主查询挂起,待子查询执行完毕,存放在临时表中以后再执行主查询。这也就是使用EXISTS比使用IN通常查询速度快的原因


*************************************结束线***********************************************


如果两个表数相当呢,那么应该用什么呢?看下面的两个列子

1)使用in

select distinct to_char(t.operate_time,'yyyy-MM-dd') as operate_time,t.machineid from jscnbi.bi_logfile t where t.machineid in (
 select distinct t.machineid
 from jscnbi.bi_logfile t
 where t.operate_time < trunc(sysdate)
 and t.operate_time >= trunc(sysdate)-1)

call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 1 0.00 0.00 0 0 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 3 52.88 51.65 32394 48663 0 2075
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 5 52.88 51.65 32394 48663 0 2075

Misses in library cache during parse: 0
Optimizer mode: ALL_ROWS
Parsing user id: 82

Rows Row Source Operation
------- ---------------------------------------------------
 2075 HASH UNIQUE (cr=48663 pr=32394 pw=0 time=51655844 us)
78208204 FILTER (cr=48663 pr=32394 pw=0 time=312853616 us)
78208204 HASH JOIN (cr=48663 pr=32394 pw=0 time=78228988 us)
 8961 TABLE ACCESS BY INDEX ROWID BI_LOGFILE (cr=3115 pr=0 pw=0 time=62808 us)
 8961 INDEX RANGE SCAN IND_BI_LOGFILE_2 (cr=27 pr=0 pw=0 time=26962 us)(object id 78233)
1132012 TABLE ACCESS FULL BI_LOGFILE (cr=45548 pr=32394 pw=0 time=1132130 us)


2)使用exits

select distinct to_char(t.operate_time,'yyyy-MM-dd') as operate_time,t.machineid from jscnbi.bi_logfile t where exists (
 select 1
 from jscnbi.bi_logfile tt
 where tt.operate_time < trunc(sysdate)-2
 and tt.operate_time >= trunc(sysdate)-3
 and tt.machineid = t.machineid)

call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 1 0.00 0.00 0 0 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 3 1.01 0.99 32519 48663 0 2075
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 5 1.02 0.99 32519 48663 0 2075

Misses in library cache during parse: 1
Optimizer mode: ALL_ROWS
Parsing user id: 82

Rows Row Source Operation
------- ---------------------------------------------------
 2075 HASH UNIQUE (cr=48663 pr=32519 pw=0 time=1002840 us)
151147 FILTER (cr=48663 pr=32519 pw=0 time=1531760 us)
151147 HASH JOIN RIGHT SEMI (cr=48663 pr=32519 pw=0 time=1078311 us)
 8961 TABLE ACCESS BY INDEX ROWID BI_LOGFILE (cr=3115 pr=1 pw=0 time=53836 us)
 8961 INDEX RANGE SCAN IND_BI_LOGFILE_2 (cr=27 pr=0 pw=0 time=17965 us)(object id 78233)
1132012 TABLE ACCESS FULL BI_LOGFILE (cr=45548 pr=32518 pw=0 time=1132114 us)
   
比较上面两个差别,发现基本上一样,但是红色的部分,是不是也就意味着差不多的时候也是用的exists呢?恩,应该是这样的!




分享到:
评论

相关推荐

    User Exits in SAP BW

    User Exits in SAP BW

    exits完全退出

    exits完全退出

    User Exits,Customer Exits,BAdI and BTE

    一个非常强的查找Customer Exit, User exit, BADI和BTE的程序 博文链接:https://jgtang82.iteye.com/blog/128474

    tor-exits:处理 Node.js 中的 Tor 出口节点

    Tor退出安装 npm install tor-exits用法 var tor = require ( 'tor-exits' ) ;tor . fetch ( function ( err , data ) { if ( err ) return console . error ( err ) ; var nodes = tor . parse ( data ) ; console ...

    2019 ICM PROBLEM D: Emergency exits. (2019 美赛 D 题: 用元胞自动机模拟逃生出口问

    2019 ICM PROBLEM D: Emergency exits. (2019 美赛 D 题: 用元胞自动机模拟逃生出口问题)

    PM USER EXITS

    SAP PM模块的用户出口总揽及介绍 ABAP语言使用

    MVS Installation exits

    IBM 大型计算机平台下进行EXIT安装教程,英文原版。

    SAP各模板增强用户出口汇总

    使用的方法是:首先定义(T-Code:CMOD)一个项目Project(以管理你的增强,这里的项目和PS模块的项目可是两回事),把你要使用的系统增加Enhancement分配给这个项目,编辑系统增强中的用户出口对象。

    Determining the stack usage of applications.pdf

    Determining the required stack sizes for a software project is a crucial part of the development process. The developer aims to create a ... when the function exits, it removes that data from the stack.

    WINCC读写SQL数据库的例子

    本代码为文章:WINCC读写SQL数据库的例子【附wincc项目文件】的实例文件。

    iOS 9 Application Development in 24 Hours(SAMS,2016)

    Printed in full color—figures and code appear as they do in Xcode 7.x • Learn to navigate the Xcode 7.x development environment and install apps on your iDevice • Get started quickly with Apple’s ...

    Borland Together key

    New (Bug 3184): Properties inspector now saves changes automatically when application exits. Fixed (Bug 4264): UML 2.0, Sequence diagram: Lifeline head changes its shape depending on the metaclass of...

    在线投票:在连接到Firebase的vuejs中进行的在线投票

    1. enter email for authenticated if exits in database then go to voting page otherwise error page. 2. voting page shows your data in database and option to select your candidate. 3. after submit page ...

    10代笔记本图纸 INTEL 10nm ICL-U RDK(1).pdf

    10代笔记本图纸 INTEL 10nm ICL-U RDK 4. * SUFFIX INDICATES ACTIVE LOW SIGNAL. 3. VCC = +5V UNLESS OTHERWISE SPECIFIED. NOTES: ...5. \I SUFFIX INDICATES SIGNAL EXITS HIERARCHICAL BLOCK.

    批量修改文件内容 bat

    可以批量替换文件里内容的小bat。可是不能零积分分享。bat内容,见下面的描述: @echo off for /f "delims=" %%i in ('dir /s/b t\\*.txt') do ( for /f "delims=" %%a in ('type "%%~fi"') do ( ...exits

    tor-exits-redis:在 redis 中维护一组 Tor 出口节点并在 Node.js 中对其运行检查

    Tor-exits-redis 维护一组 Tor 出口节点并对其进行检查安装npm install tor-exits-redis 用法更新(选项,CB) 创建/附加到一组 Tor 出口节点检查(IP,选项,CB) 检查给定的 ip 地址是否是已知的 Tor 出口节点

    关于SAP的用户出口

    用户出口就是SAP中的Customer Exits或者User Exits 什么叫用户出口呢?打个比方说吧,SAP软件就象一根晾衣服的绳子,上面有数不清的衣架,多数衣架上已经挂上了衣服,就些衣服就SAP的标准程序,还有些衣架是空着的...

    hex2bin_2.2_XiaZaiBa.zip

    -b Batch mode: exits if specified file doesn't exist -c Enable record checksum verification -C [Poly][Init][RefIn][RefOut][XorOut] CRC parameters -e [ext] Output filename extension (without ...

    sap 常见用户出口

    用户出口就是SAP中的Customer Exits或者User Exits 什么叫用户出口呢?打个比方说吧,SAP软件就象一根晾衣服的绳子,上面有数不清的衣架,多数衣架上已经挂上了衣服,就些衣服就SAP的标准程序,还有些衣架是空着的...

Global site tag (gtag.js) - Google Analytics