oracle-exists和in的区别

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+实用教程》

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值