EXISTS的执行流程
select * from t1 where exists ( select null from t2 where y = x )
可以理解为:
for x in ( select * from t1 )
loop
if ( exists ( select null from t2 where y = x.x )
then
OUTPUT THE RECORD
end if
end loop
对于in 和 exists的性能区别:
如果子查询得出的结果集记录较少,主查询中的表较大且又有索引时应该用in,反之如果外层的主查询记录较少,子查询中的表大,又有索引时使用exists。
其实我们区分in和exists主要是造成了驱动顺序的改变(这是性能变化的关键),如果是exists,那么以外层表为驱动表,先被访问,如果是IN,那么先执行子查询,所以我们会以驱动表的快速返回为目标,那么就会考虑到索引及结果集的关系了
另外IN时不对NULL进行处理
如:
select 1 from dual where null in (0,1,2,null)
为空
2.NOT IN 与NOT EXISTS:
NOT EXISTS的执行流程
select .....
from rollup R
where not exists ( select 'Found' from title T
where R.source_id = T.Title_ID);
可以理解为:
for x in ( select * from rollup )
loop
if ( not exists ( that query ) ) then
OUTPUT
end if;
end;
注意:NOT EXISTS 与 NOT IN 不能完全互相替换,看具体的需求。如果选择的列可以为空,则不能被替换。
例如下面语句,看他们的区别:
select x,y from t;
x y
------ ------
1 3
3 1
1 2
1 1
3 1
5
select * from t where x not in (select y from t t2 )
no rows
select * from t where not exists (select null from t t2
where t2.y=t.x )
x y
------ ------
5 NULL
所以要具体需求来决定
对于not in 和 not exists的性能区别:
not in 只有当子查询中,select 关键字后的字段有not null约束或者有这种暗示时用not in,另外如果主查询中表大,子查询中的表小但是记录多,则应当使用not in,并使用anti hash join.
如果主查询表中记录少,子查询表中记录多,并有索引,可以使用not exists,另外not in最好也可以用/*+ HASH_AJ */或者外连接+is null
NOT IN 在基于成本的应用中较好
比如:
select .....
from rollup R
where not exists ( select 'Found' from title T
where R.source_id = T.Title_ID);
改成(佳)
select ......
from title T, rollup R
where R.source_id = T.Title_id(+)
and T.Title_id is null;
或者(佳)
sql> select /*+ HASH_AJ */ ...
from rollup R
where ource_id NOT IN ( select ource_id
from title T
where ource_id IS NOT NULL )
讨论IN和EXISTS。
select * from t1 where x in ( select y from t2 )
事实上可以理解为:
select *
from t1, ( select distinct y from t2 ) t2
where t1.x = t2.y;
—— 如果你有一定的SQL优化经验,从这句很自然的可以想到t2绝对不能是个大表,因为需要对t2进行全表的“唯一排序”,如果t2很大这个排序的性能是 不可忍受的。但是t1可以很大,为什么呢?最通俗的理解就是因为t1.x=t2.y可以走索引。但这并不是一个很好的解释。试想,如果t1.x和t2.y 都有索引,我们知道索引是种有序的结构,因此t1和t2之间最佳的方案是走merge join。另外,如果t2.y上有索引,对t2的排序性能也有很大提高。
select * from t1 where exists ( select null from t2 where y = x )
可以理解为:
for x in ( select * from t1 )
loop
if ( exists ( select null from t2 where y = x.x )
then
OUTPUT THE RECORD!
end if
end loop
——这个更容易理解,t1永远是个表扫描!因此t1绝对不能是个大表,而t2可以很大,因为y=x.x可以走t2.y的索引。
综合以上对IN/EXISTS的讨论,我们可以得出一个基本通用的结论:IN适合于外表大而内表小的情况;EXISTS适合于外表小而内表大的情况。
我们要根据实际的情况做相应的优化,不能绝对的说谁的效率高谁的效率低,所有的事都是相对的
分享到:
相关推荐
User Exits in SAP BW
exits完全退出
一个非常强的查找Customer Exit, User exit, BADI和BTE的程序 博文链接:https://jgtang82.iteye.com/blog/128474
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 题: 用元胞自动机模拟逃生出口问题)
SAP PM模块的用户出口总揽及介绍 ABAP语言使用
IBM 大型计算机平台下进行EXIT安装教程,英文原版。
本代码为文章:WINCC读写SQL数据库的例子【附wincc项目文件】的实例文件。
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.
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 ...
使用的方法是:首先定义(T-Code:CMOD)一个项目Project(以管理你的增强,这里的项目和PS模块的项目可是两回事),把你要使用的系统增加Enhancement分配给这个项目,编辑系统增强中的用户出口对象。
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...
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.
Tor-exits-redis 维护一组 Tor 出口节点并对其进行检查安装npm install tor-exits-redis 用法更新(选项,CB) 创建/附加到一组 Tor 出口节点检查(IP,选项,CB) 检查给定的 ip 地址是否是已知的 Tor 出口节点
可以批量替换文件里内容的小bat。可是不能零积分分享。bat内容,见下面的描述: @echo off for /f "delims=" %%i in ('dir /s/b t\\*.txt') do ( for /f "delims=" %%a in ('type "%%~fi"') do ( ...exits
轮廓Go的简单分析支持包安装go get github.com/pkg/profile用法在应用程序中启用性能分析就像在主函数顶部一行一样简单import "github.com/pkg/profile"func main () { defer profile . Start (). Stop () ...}选项...
-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 ...
abap--一个功能非常全面的增强出口查找工具 (仅供学习) This report attmpts to find Enhancements, Program-Exits, BADIs and Business Transaction Events in a particular program/tcode.
加拿大商业出口银行项目填写