in和exists、not in和not exists性能对比
来源:互联网 发布:新疆为什么没有4g网络 编辑:程序博客网 时间:2024/06/07 03:51
In是把外表和内表做Hash 连接,而exists 是对外表作loop 循环,每次loop循环再对内表进行查询。
当查询两个表的大小相当时,用In 和 exists差别不大。
如果两个表中一个表较小,一个表较大,那么子查询表大的用exists,子查询表小的用In,效率会高的。
也就是说 IN适合于外表大而内表小的情况;EXISTS适合于外表小而内表大的情况,这样效率会高的
例如 :表a(小表),表b(大表)
1.select * from a where aid in (select aid from b) --->效率低:全程扫描b表,用到a 表上的aid的索引,因为a表小,b表大
上面in的语句可以理解为:
select *
from a, ( select distinct aid from b) b1
where a.aid = b1.aid.
select * from a where exists (select aid from b where b.aid = a.aid) --->效率高: 全程扫描a表,用到b表上的aid的索引。因为b表大。
上面的exists的语句可以理解为:
for aid in ( select * from a)
loop
if ( exists ( select aid from b where b.aid= a.aid )
then
OUTPUT THE RECORD!
end if
end loop
2. select * from b where aid in (select aid from a)----效率高:全程扫描a 表,用到b表上的aid 索引
select * from b where exists (select aid from a were a.aid= b.aid) --->效率低:全程扫描b 表:用到a 表上的aid索引。
Not in 和Not Exists 的 效率
如果查询语句使用了Not In,那么内外表全部进行扫描,没有乃至索引
Not Exist用到子表中的索引进行查询,所以无论两个表中哪个表大,Not exists 都要比Not in 要快。
0 0
- in和exists、not in和not exists性能对比
- mysql not exists 和 not in对比
- exists,in 和 not in ,not exists
- not exists 和not in
- not in 和 not exists
- exists和not exists用法 代替in和not in
- in 和 exists , not in 和 not exists
- exists和not exists用法 代替in和not in
- in和exists,not in和not exists
- in 和 exists 区别,not in和not exists区别
- oracle in和exists、not in和not exists原理和性能探究
- IN,NOT IN,EXISTS,NOT EXISTS的用法和差别
- exists 与not exists in 和not in用法
- exists\not exists和in\not in效率
- 对于not in 和 not exists的性能区别:
- not in和not exists的区别
- not in 和not exists的使用
- NOT IN和NOT EXISTS小区别
- Spring MVC 拦截器
- 【微服务】【概念】【原则】【连接】
- [蓝桥杯]数组排序
- nginx之rtmp模块引用计数设计
- Android 获取常用的系统及应用的版本信息
- in和exists、not in和not exists性能对比
- Outlook2007 设置Subject未添加提示和附件未添加成功提示
- 试着用React写项目-利用react-router解决跳转路由等问题(一)
- MtK Camera FAQ
- [高性能MySQL]-事务与隔离界别
- 获取到listView的高度
- 拉格朗日乘子法及KKT条件证明
- MySQL进阶漂流记(六)
- Openfiler 创建NAS存储