码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-10-10 19:03:55      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:子查询   exists   for   表达式   其他   没有   对比   问题   select   

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来代替,何种最优需要具体问题具体分析。

 

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

标签:子查询   exists   for   表达式   其他   没有   对比   问题   select   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!