6.2 小表驱动大表(exists的应用)

1. 优化原则:小表驱动大表,即小数据集驱动大数据集。

select * from A where id in (select id from B)
等价于:
for select id from B
for select * from A where A.id = B.id

当B表的数据集必须小于A的数据集时,用in优于exists。

select * from A where exists (select 1 from B where B.id = A.id)
等价于:
for select * from A
for select * from B where B.id = A.id

当A表的数据集系小于B表的数据集时,用exists优于in。

注意:A表于B表的ID字段上应建立索引。

2. exists

select ... from table where exists (subquery)
该语句可以理解为:将主查询的数据,放到子查询中做条件验证,根据验证结果(TRUE 或FALSE)来决定主查询的数据结果是否得以保留。
1. exists (subquery) 只返回true或false,因此子查询中的select * 也可以是select 1 或 select 'X',官方说法是实际执行是会忽略select清单,因此没有区别。
2. exists 子查询的实际执行过程可能经过优化而不是我们理解上的逐条对比,如果担心效率问题,可以进行实际检验以确定是否有效率问题。
3. exists 子查询往往也可以用条件表达式、其他查询或者JOIN来代替,何种最优需要具体问题具体分析。

关注我的公众号,精彩内容不能错过

原文地址:https://www.cnblogs.com/huanchupkblog/p/7646908.html