oracle in exists 区别
来源:互联网 发布:360家庭网络修复在哪里 编辑:程序博客网 时间:2024/06/05 08:26
在Oracle SQL中取数据时有时要用到in 和 exists 那么他们有什么区别呢?
1 性能上的比较
比如Select * from T1 where x in ( select y from T2 )
执行的过程相当于:
select *
from t1, ( select distinct y from t2 ) t2
where t1.x = t2.y;
相对的
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 不可避免的要被完全扫描一遍
分别适用在什么情况?
以子查询 ( select y from T2 )为考虑方向
如果子查询的结果集很大需要消耗很多时间,但是T1比较小执行( select null from t2 where y = x.x )非常快,那么exists就比较适合用在这里
相对应得子查询的结果集比较小的时候就应该使用in.
转载自http://hi.baidu.com/hopedaily/blog/item/56d23edbde87cd60d0164efe.html
in和exists
in 是把外表和内表作hash 连接,而exists是对外表作loop循环,每次loop循环再对内表进行查询。
一直以来认为exists比in效率高的说法是不准确的。
如果查询的两个表大小相当,那么用in和exists差别不大。
如果两个表中一个较小,一个是大表,则子查询表大的用exists,子查询表小的用in:
例如:表A(小表),表B(大表)
1:
select * from A where cc in (select cc from B)
效率低,用到了A表上cc列的索引;
select * from A where exists(select cc from B where cc=A.cc)
效率高,用到了B表上cc列的索引。
相反的
2:
select * from B where cc in (select cc from A)
效率高,用到了B表上cc列的索引;
select * from B where exists(select cc from A where cc=B.cc)
效率低,用到了A表上cc列的索引。
in 与 =的区别
select name from student where name in ('zhang','wang','li','zhao');
与
select name from student where name='zhang' or name='li' or name='wang' or name='zhao'
的结果是相同的。
in 内表全扫,exists 外表全扫
in 用到外表索引(外表大就快) exists用到子查询索引(子大就快)
not in 和not exists
如果查询语句使用了not in 那么内外表都进行全表扫描,没有用到索引;
而not extsts 的子查询依然能用到表上的索引。
所以无论那个表大,用not exists都比not in要快。
1 性能上的比较
比如Select * from T1 where x in ( select y from T2 )
执行的过程相当于:
select *
from t1, ( select distinct y from t2 ) t2
where t1.x = t2.y;
相对的
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 不可避免的要被完全扫描一遍
分别适用在什么情况?
以子查询 ( select y from T2 )为考虑方向
如果子查询的结果集很大需要消耗很多时间,但是T1比较小执行( select null from t2 where y = x.x )非常快,那么exists就比较适合用在这里
相对应得子查询的结果集比较小的时候就应该使用in.
转载自http://hi.baidu.com/hopedaily/blog/item/56d23edbde87cd60d0164efe.html
in和exists
in 是把外表和内表作hash 连接,而exists是对外表作loop循环,每次loop循环再对内表进行查询。
一直以来认为exists比in效率高的说法是不准确的。
如果查询的两个表大小相当,那么用in和exists差别不大。
如果两个表中一个较小,一个是大表,则子查询表大的用exists,子查询表小的用in:
例如:表A(小表),表B(大表)
1:
select * from A where cc in (select cc from B)
效率低,用到了A表上cc列的索引;
select * from A where exists(select cc from B where cc=A.cc)
效率高,用到了B表上cc列的索引。
相反的
2:
select * from B where cc in (select cc from A)
效率高,用到了B表上cc列的索引;
select * from B where exists(select cc from A where cc=B.cc)
效率低,用到了A表上cc列的索引。
in 与 =的区别
select name from student where name in ('zhang','wang','li','zhao');
与
select name from student where name='zhang' or name='li' or name='wang' or name='zhao'
的结果是相同的。
in 内表全扫,exists 外表全扫
in 用到外表索引(外表大就快) exists用到子查询索引(子大就快)
not in 和not exists
如果查询语句使用了not in 那么内外表都进行全表扫描,没有用到索引;
而not extsts 的子查询依然能用到表上的索引。
所以无论那个表大,用not exists都比not in要快。
0 0
- oracle in exists 区别
- Oracle exists 和 in 的真正区别
- oracle in与exists语句的区别
- ORACLE中in 和 exists区别
- oracle 中 in ,EXISTs 的区别
- oracle中in和exists的区别
- Oracle exists 和 in 的真正区别
- ORACLE中in 和 exists区别
- Oracle中的in和exists区别
- Oracle中的in和exists区别二
- Oracle中exists与in区别
- oracle中的exists和in的区别
- ORACLE in与exists语句的区别
- Oracle in和exists的区别
- ORACLE 中IN和EXISTS区别
- ORACLE in与exists语句的区别
- oracle中in和exists的区别
- ORACLE in与exists语句的区别
- 【转】一个fork的面试题
- 【原创】Bash下Ctrl-C、Ctrl-D和Ctrl-Z的区别
- mysql一对多关联查询
- sublime每次打开时都提示升级,怎么取消这个弹出框?
- Decimal integer conversion
- oracle in exists 区别
- SnackBar使用详解
- Python数据存储之MySQL(下)
- 求1+2+…+n
- QT之在toolButton中添加图标和文字,使文字显示在图标下方
- 封装自己的JS组件
- MFC 基于对话框的 OnCommand 用作响应按钮事件
- https证书
- 【原创】unset和null的区别