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文的效果是不是完全一样?
第一个是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
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28