In和Exists的优化案例讲解
Demo Table
in的逻辑
优化原则
exists的逻辑
Demo Table
1 | CREATE TABLE t1 ( |
两个表t1 和 t2 , 一样的,包括索引信息
数据量t1 ,t2 如下
1 | mysql> select count(1) from t1; |
in的逻辑
1 | select * from t1 where id in (select id from t2) ; |
这个SQL,先执行哪个呢?
看看执行计划

可以理解为
1 | for(select id from t2){ |
优化原则
原则:小表驱动大表,即小的数据集驱动大的数据集
当T2表的数据集小于T1表的数据集时,in优于exists
exists的逻辑
1 | select * from A where exists (select 1 from B where B.id = A.id) |
可以理解为
1 | for(select * from A){ |
当A表的数据集小于B表的数据集时,exists优于in
将主查询A的数据,放到子查询B中做条件验证,根据验证结果(true或false)来决定主查询的数据是否保留
1、 EXISTS(subquery)只返回TRUE或FALSE,因此子查询中的SELECT*也可以用SELECT1替换,官方说法是实际执行时会忽略SELECT清单,因此没有区别;
2、 EXISTS子查询的实际执行过程可能经过了优化而不是我们理解上的逐条对比;
3、 EXISTS子查询往往也可以用JOIN来代替,何种最优需要具体问题具体分析;
1 | mysql> explain select * from t2 where exists (select 1 from t1 where t1.id = t2.id) ; |