您的位置:首页 > 数据库 > Oracle

oracle-exists和in的区别

2017-12-22 10:25 369 查看
1.exists

select * from table1 where exists (select null from table2 where a = b)


可以写为:

for x in (select * from table1)
loop
if(exists (select null from t2 where a = b))
then
output the record
endif
end loop


in 和 exists的区别在于:

exists 是先查找主查询,in是先查找子查询

如果子查询比较少,组查询表大&有索引, 应该用 in

反之用exists

其实区分in和exists 主要是驱动顺序的改变(这是性能变化的关键),

如果是exists,那么外层表为驱动表,如果是in则先执行子查询,所以我们会以驱动表的快速返回为目标,那么就会考虑到索引及结果集的关系了。

ps: in 是不对null 进行处理的

select 1 from dual where null in (0,1,2,null)
--查询结果为空


注:读自《oracle+11g+实用教程》
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: