第一篇文章的说法
in是把外表和内表作hash连接,而exists是对外表作loop循环,每次loop循环再对内表进行查询,一直以来认为exists比in效率高的说法是不准确的。如果查询的两个表大小相当,那么用in和exists差别不大;如果两个表中一个较小一个较大,则子查询表大的用exists,子查询表小的用in;
例如:表A(小表),表B(大表)
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列的索引。
相反的:
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列的索引。
另外一文章
1 select * from A where id in(select id from B)
以上查询使用了in语句,in()只执行一次,它查出B表中的所有id字段并缓存起来.之后,检查A表的id是否与B表中的id相等,如果相等则将A表的记录加入结果集中,直到遍历完A表的所有记录.
它的查询过程类似于以下过程
1 List resultSet=[]; 2 Array A=(select * from A); 3 Array B=(select id from B); 4 5 for(int i=0;i<A.length;i++) { 6 for(int j=0;j<B.length;j++) { 7 if(A[i].id==B[j].id) { 8 resultSet.add(A[i]); 9 break; 10 } 11 } 12 } 13 return resultSet;
可以看出,当B表数据较大时不适合使用in(),因为它会B表数据全部遍历一次.
如:A表有10000条记录,B表有1000000条记录,那么最多有可能遍历10000*1000000次,效率很差.
再如:A表有10000条记录,B表有100条记录,那么最多有可能遍历10000*100次,遍历次数大大减少,效率大大提升.
结论:in()适合B表比A表数据小的情况
1 select a.* from A a 2 where exists(select 1 from B b where a.id=b.id)
以上查询使用了exists语句,exists()会执行A.length次,它并不缓存exists()结果集,因为exists()结果集的内容并不重要,重要的是结果集中是否有记录,如果有则返回true,没有则返回false.
它的查询过程类似于以下过程
1 List resultSet=[]; 2 Array A=(select * from A) 3 4 for(int i=0;i<A.length;i++) { 5 if(exists(A[i].id) { //执行select 1 from B b where b.id=a.id是否有记录返回 6 resultSet.add(A[i]); 7 } 8 } 9 return resultSet;
当B表比A表数据大时适合使用exists(),因为它没有那么遍历操作,只需要再执行一次查询就行.
如:A表有10000条记录,B表有1000000条记录,那么exists()会执行10000次去判断A表中的id是否与B表中的id相等.
如:A表有10000条记录,B表有100000000条记录,那么exists()还是执行10000次,因为它只执行A.length次,可见B表数据越多,越适合exists()发挥效果.
再如:A表有10000条记录,B表有100条记录,那么exists()还是执行10000次,还不如使用in()遍历10000*100次,因为in()是在内存里遍历比较,而exists()需要查询数据库,我们都知道查询数据库所消耗的性能更高,而内存比较很快.
结论:exists()适合B表比A表数据大的情况
当A表数据与B表数据一样大时,in与exists效率差不多,可任选一个使用.
第三篇文章:
1 性能上的比较
比如Select * from T1 where x in ( select y from T2 )
执行的过程相当于:
1 select * 2 from t1, ( select distinct y from t2 ) t2 3 where t1.x = t2.y;
相对的
select * from t1 where exists ( select null from t2 where y = x )
执行的过程相当于:
1 for x in ( select * from t1 ) 2 loop 3 if ( exists ( select null from t2 where y = x.x ) 4 then 5 OUTPUT THE RECORD 6 end if 7 end loop
表 T1 不可避免的要被完全扫描一遍
分别适用在什么情况?
以子查询 ( select y from T2 )为考虑方向
如果子查询的结果集很大需要消耗很多时间,但是T1比较小执行( select null from t2 where y = x.x )非常快,那么exists就比较适合用在这里
相对应得子查询的结果集比较小的时候就应该使用in.
第四篇文章:
1 update p_container_decl cd 2 set cd.ANNUL_FLAG='0001',ANNUL_DATE = sysdate 3 where exists( 4 select 1 5 from ( 6 select tc.decl_no,tc.goods_no 7 from p_transfer_cont tc,P_AFFIRM_DO ad 8 where tc.GOODS_DECL_NO = ad.DECL_NO 9 and ad.DECL_NO = 'sssssssssssssssss' 10 ) a 11 where a.decl_no = cd.decl_no 12 and a.goods_no = cd.goods_no 13 )
上面涉及的3个表的记录数都不小,均在百万左右。根据这种情况,我想到了前不久看的tom的一篇文章,说的是exists和in的区别,
in 是把外表和那表作hash join,而exists是对外表作loop,每次loop再对那表进行查询。
这样的话,in适合内外表都很大的情况,exists适合外表结果集很小的情况。
而我目前的情况适合用in来作查询,于是我改写了sql,如下:
1 update p_container_decl cd 2 set cd.ANNUL_FLAG='0001',ANNUL_DATE = sysdate 3 where (decl_no,goods_no) in 4 ( 5 select tc.decl_no,tc.goods_no 6 from p_transfer_cont tc,P_AFFIRM_DO ad 7 where tc.GOODS_DECL_NO = ad.DECL_NO 8 and ad.DECL_NO = ‘ssssssssssss’ 9 )
让市场人员测试,结果运行时间在1分钟内。问题解决了,看来exists和in确实是要根据表的数据量来决定使用。
备注:
http://blog.csdn.net/lick4050312/article/details/4476333#comments
http://www.cnblogs.com/seasons1987/archive/2013/07/03/3169356.html
http://www.111cn.net/database/mssqlserver/42359.htm