in 是把外表和内表作hash 连接,而exists是对外表作loop循环,每次loop循环再对内表进行查询。
一直以来认为exists比in效率高的说法是不准确的。
如果查询的两个表大小相当,那么用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列的索引。
not in 和not exists
如果查询语句使用了not in 那么内外表都进行全表扫描,没有用到索引;
而not extsts 的子查询依然能用到表上的索引。
所以无论那个表大,用not exists都比not in要快。
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'
的结果是相同的。
-----------------------------------------------------------------实例
--登陆gather/123456@pc150 执行以下语句,注意执行时间
---小表
er_street_code
select distinct tt.gkdm from er_street_code tt where tt.gkdm is not null
---大表
er_in_detail,er_reck_detail
---例1
select * from er_reck_detail t1 where t1.gkdm in (select gg.gkdm from er_street_code gg)
select * from er_reck_detail t1 where exists (select * from er_street_code gg where t1.gkdm=gg.gkdm)
---例2
select * from er_street_code aa where aa.gkdm in (select mm.gkdm from er_reck_detail mm)
select * from er_street_code aa where exists (select * from er_reck_detail mm where mm.gkdm=aa.gkdm)
---例3
select * from er_reck_detail t1 where t1.gkdm in (select t2.gkdm from er_in_detail t2)
select * from er_reck_detail t1 where exists (select * from er_in_detail t2 where t1.gkdm=t2.gkdm)
本篇文章来源于:开发学院 http://edu.codepub.com 原文链接:http://edu.codepub.com/2009/0721/10539.php
分享到:
相关推荐
golang 错误提示 $GOPATH/go.mod exists but should not goland 出现该问题 去掉vendor即可 初出茅庐有错必改 欢迎交流
里面自己根据网上的资源整理出来的一份sql中in,exists,not in,not exists的使用方法以及注意事项等,有助于初学的朋友们借鉴。
关于Exists,not Exists.in ,not in效率的说明。关于Exists,not Exists.in ,not in效率的说明。关于Exists,not Exists.in ,not in效率的说明。关于Exists,not Exists.in ,not in效率的说明。关于Exists,not Exists.in...
一直以来,大家认为exists比in速度快,其实是不准确的。且看接下来的具体分析:in其实是将外表和内表进行hash join,exists是先对外表进行loop操作,然后每次loop后再对内表进行查询。 如果两张表大小差不多,那么...
一个是问in exist的区别,一个是not in和not exists的区别
SQL语句优化——in,not in,exists,not exists, left join...on博客所需SQL语句.txt欢迎下载!
oracle in和exists性能解析
“exists”和“in”是Oracle中,都是查询某集合的值是否存在在另一个集合,但对不同的数据有不同的用法,主要是在效率问题上存在很大的差别,以下有两个简单例子,以说明 “exists”和“in”的效率问题。
oracle数据库关于exists使用方法与in的比较
oracle中exists_和in的效率问题详解
MySQL优化之in,exists,not in,not exists的区别in与existsin查询过程结论:exists查询过程:结论:not in与not existsnot in查询过程:结论:not exists查询过程:结论: 首先我们使用两个用户表作为实例 insert ...
sql语句优化之用EXISTS替代IN、用NOT EXISTS替代NOT IN的语句
NOT EXISTS ⇔ NOT IN ⇒ NOT NULL 问题 结果不一样 疑问 如果等效
NULL 博文链接:https://576017120.iteye.com/blog/1624774
系统要求进行SQL优化,对效率比较低的SQL进行优化,使其运行效率更高,其中要求对SQL中的部分in/not in修改为exists/not
exists 和 not exists的详细解释
NOT IN、JOIN、IS NULL、NOT EXISTS效率对比 语句一:select count(*) from A where A.a not in (select a from B) 语句二:select count(*) from A left join B on A.a = B.a where B.a is null 语句三:select ...
SQL语句优化——in,not in,exists,not exists, left join...on博客所需SQL语句2.txt,欢迎下载!