SQL优化-IN和EXITS

来源:互联网 发布:刺客信条3优化好吗 编辑:程序博客网 时间:2024/04/29 05:48
INEXITS

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);
  通过理解
INEXISTSNOT 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_ajmerge_aj连接。

0 0
原创粉丝点击