浅析SQL中 in 与 exists 用法的区别及其各自执行流程、not in/not exists区别、sql优化应该如何选择in还是exists

本文详细分析了SQL中in与exists的区别,包括它们的查询执行流程、性能对比以及not in与not exists的区别。in适合子查询结果小的情况,exists适合主查询数据大的情况。对于查询优化,应根据表数据大小选择合适的操作符。
摘要由CSDN通过智能技术生成

一、in 与 exists 的区别

1、exists、not exists 一般都是与子查询一起使用,In 可以与子查询一起使用,也可以直接in (a,b…)

2、exists 会针对子查询的表使用索引,not exists 会对主子查询都会使用索引。in 与子查询一起使用的时候,只能针对主查询使用索引,not in 则不会使用任何索引。

注意:一直以来认为 exists 比 in 效率高的说法是不准确的。

in 是把外表和内表作 hash 连接,而 exists 是对外表作 loop 循环,每次 loop 循环再对内表进行查询。

如果查询的两个表大小相当,那么用 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 in实质上等于​​!= and != …​​,因为 != 不会使用索引,故 not in 不会使用索引。

所以无论那个表大,用 not exists 都比 not in 要快。

3、exists 与 in 都可以实现一个目的,二者都可以用来过滤数据。

select count(1) from t1;     --160W
select count(1) from t2;     --90W

SELECT count(1)
FROM t1 a
WHERE EXISTS (SELECT accountid
FROM t2 b
WHERE a.keyid = b.keyid AND a.ideaid = b.ideaid);
--主大子小,不适合使用exist,因为exist只会利用子表t2的复合索引keyid+ideaid,而子表内容要小与主表,主表由于无法使用索引,查询效率低下.

select count(1) from t1 a where a
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值