+ -
当前位置:首页 → 问答吧 → exists或者是in的子查询条件,是否可以放在本身的条件里

exists或者是in的子查询条件,是否可以放在本身的条件里

时间:2011-12-16

来源:互联网

例如:

第一个是in的sql文:
select * from a
where a.a1 in
(select b.b1 from b
 where a.a1=b.b1
  and a.a2='AAA' and a.a3 between b.b3 and b.b4  

and a.a4 > 0


第二个是exists的sql文:
select * from a
where exists
(select 1 from b
 where a.a1=b.b1
  and a.a2='AAA' and a.a3 between b.b3 and b.b4  
 )
and a.a4 > 0

第三个是把第一个sql文in里的子查询的条件拿出来:
select * from a
where a.a1 in
(select b.b1 from b
 where a.a1=b.b1
  and a.a3 between b.b3 and b.b4  

and a.a4 > 0
and a.a2='AAA'
第四个是把第二个sql文exists里的子查询的条件拿出来:
select * from a
where exists
(select 1 from b
 where a.a1=b.b1
  and a.a3 between b.b3 and b.b4  
 )
and a.a4 > 0
and a.a2='AAA'
以上4个sql文的效果是不是完全一样?




作者: zhayifeng   发布时间: 2011-12-16

应该是一样的

作者: youqi1984   发布时间: 2011-12-16

效率不一样 结果应该是一样的

作者: woshiliushanj   发布时间: 2011-12-16

引用 2 楼 woshiliushanj 的回复:
效率不一样 结果应该是一样的


请问,如果a表数据多,b表少,按照一般的理解,这样的话in的性能优于exists,也就是sql3性能好于sql4,sql1性能好于sql2。但sql3和sql1的性能又如何判断

作者: zhayifeng   发布时间: 2011-12-16

热门下载

更多