首页 > 代码库 > SQL语句Not IN优化方案

SQL语句Not IN优化方案

总结网友们在CSDN社区上对于not in的优化策略,整理如下,备查。 
 
select * from emp where emp_no not in (select emp_no from emp_bill)
要求用两种 SQL 写法优化上面 SQL 。
 
方法一、
 
select *
 
       from emp a
 
        where   not exists ( select 1
 
       from emp_bill b
 
          where b.emp_no = a.emp_no)
 
方法二、
 
select a.*
 
       from emp a ,emp_bill b
 
       where a.emp_no=b.emp_no(+)
 
             and b.emp_no is null
 
方法二继续优化、
 
select a.*
 
       from emp a ,emp_bill b
 
       where a.emp_no=b.emp_no(+)
 
             and NVL(b.emp_no, ‘1‘) = ‘1‘ 
 
表连接效率最好, Not Exists 其次, Not in 最低

SQL语句Not IN优化方案