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 )
注意:上面只是从理论上提出了一些建议,最好的原则是大家在上面的基础上,能够使用执行计划来分析,得出最佳的语句的写法
希望大家提出异议
分享到:
相关推荐
oracle数据库关于exists使用方法与in的比较
“exists”和“in”是Oracle中,都是查询某集合的值是否存在在另一个集合,但对不同的数据有不同的用法,主要是在效率问题上存在很大的差别,以下有两个简单例子,以说明 “exists”和“in”的效率问题。
oracle in和exists性能解析
oracle中exists_和in的效率问题详解
里面自己根据网上的资源整理出来的一份sql中in,exists,not in,not exists的使用方法以及注意事项等,有助于初学的朋友们借鉴。
一直以来,大家认为exists比in速度快,其实是不准确的。且看接下来的具体分析:in其实是将外表和内表进行hash join,exists是先对外表进行loop操作,然后每次loop后再对内表进行查询。 如果两张表大小差不多,那么...
一个是问in exist的区别,一个是not in和not exists的区别
NULL 博文链接:https://wuaner.iteye.com/blog/1671927
Using a Subquery in the FROM Clause 4-10 Scalar Subquery Expressions 4-11 Scalar Subqueries: Examples 4-12 Correlated Subqueries 4-14 Using Correlated Subqueries 4-16 Using the EXISTS Operator 4-18 ...
oracle进阶教程 1、优化器 2、连接 3、in和exists 4、hint 5、index 6、收集统计信息 7、优化技巧
尽量少用IN操作符,基本上所有的IN操作符都可以用EXISTS代替 用IN写出来的SQL的优点是比较容易写及清晰易懂,但是用IN的SQL性能总是比较低的,从ORACLE执行的步骤来分析用IN的SQL与不用IN的SQL有以下区别:...
with子句使用,集合操作,case应用,行列互换,4 exists与in、not exists与not in
尽量用not exists 或者外连接替代 not in 操作符。因为not in不能 应用表的索引 3。尽量不用<> 或者!= 操作符。不等于操作符是永远不会用到索引的,因此 对它的处理只会产生全表扫描,改为 a> XX or a 4.在设计...
§12.5 使用EXISTS和IN 148 §12.6 分离事务(Discrete Transactions ) 149 §12.7 测试SQL语句性能 151 §12.7.1 SQL_Trace实用工具 151 §12.7.2 TKPROF实用程序 151 §12.8 使用SQL_Trace和TKPROF 151 §12.8.1 ...
oracle语句优化:多表关联,exists ,in ORACLE对每个SQL都会对其进行分析,占用共享内存,服务器当前性能(共享内存、磁盘文件碎片)、数据库对象(表、索引)统计信息
WHERE group_number IN (select group_number from v$asm_diskgroup where name = 'ORCL_DATA1'); PATH ---------------------------------- ORCL:VOL1 ORCL:VOL2 二: 启动/停止RAC集群 确保是以 oracle ...
12.7 in和exists比较 22 十三、 多表查询 23 13.1按范式要求设计表结构 23 13.2多表连接的种类 23 13.3交叉连接 23 13.4内连接 23 13.5外连接 25 13.6非等值连接 27 13.7表连接总结 27 十四、 集合 28 14.1表连接...
用Exist或Not Exists来代理In。In进行子查询效率很差。 5.SQL语句分析 通过SQLPLUS中的SET TRACE 功能对Sql语句的性能进行分析 通过Toad或PL/SQL Developer对语句的性能进行和索引的使用情况进行分析 对Oracle...