SQL优化-IN和EXITS

IN EXITS

NOT IN 语句实质上等同于使用 = 比较每一值,如果测试为 FALSE 或者 NULL ,结果为比较失败。例如
  
select 'true' from dual where 1 not in (null,2);
  
select 'true' from dual where 1 != null and 1 != 2;
  
select 'true' from dual where (1,2) not in ((2,3),(2,null));
  
select 'true' from dual where (1,null) not in ((1,2),(2,3));
  这些查询不会返回任何一行。第二个查询语句更为明显,即
1 != null ,所以整个 WHERE 都为 false 。然而这些查询语句可变为:
 
  
select 'true' from dual where 1 not in (2,3);
  
select 'true' from dual where 1 != 2 and 1 != 3;
 
  你也可以使用
NOT IN 查询,只要你保证返回的值不会出现 NULL 值:
  
  
select ename from emp where empno not in (select mgr from emp where mgr is not   null );
  
select ename from emp where empno not in (select nvl(mgr,0) from emp);
  通过理解
IN EXISTS NOT IN ,以及 NOT EXISTS 之间的差别,当 NULL 出现在任一子查询中时,你可以避免一些常见的问题




not in 逻辑上不完全等同于 not exists
请注意 not in 逻辑上不完全等同于 not exists ,如果你误用了 not in ,小心你的程序存在致命的 BUG

请看下面的例子:
create table t1 (c1 number,c2 number);
create table t2 (c1 number,c2 number);

insert into t1 values (1,2);
insert into t1 values (1,3);
insert into t2 values (1,2);
insert into t2 values (1,null);

select * from t1 where c2 not in (select c2 from t2);
no rows found
select * from t1 where not exists (select 1 from t2 where t1.c2=t2.c2);
c1 c2
1 3

正如所看到的, not in 出现了不期望的结果集,存在逻辑错误。如果看一下上述两个 select 语句的执行计划,也会不同。后者使用了 hash_aj
因此,请尽量不要使用
not in( 它会调用子查询 ) ,而尽量使用 not exists( 它会调用关联子查询 ) 。如果子查询中返回的任意一条记录含有空值,则查询将不返回任何记录,正如上面例子所示。
除非子查询字段有非空限制,这时可以使用
not in , 并且也可以通过提示让它使用 hasg_aj merge_aj 连接。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值