有两表a和表b
两表结构相同,如何优化比较显示b表中不存在的记录
id是主键a:
id name ....
--------------
1  aaa
2  bbb
3  ccc
4  ddd
5  32a
6  4s2
7  433b:
id name ....
------------
1  aaa
2  bbb
3  ccc
4  ddd得到的结果为id name ....
------------5  32a
6  4s2
7  433如何优化?现在使用 select * from a where not exists (select * from b where a.id=b.id)
有什么方法可以提高效率.

解决方案 »

  1.   

    select * from a
    minus
    select * from b
      

  2.   

    select a.* from a ,b
    where a.id = b.id(+)
      and b.id is null
      

  3.   

    select a.id,a.name from a
    where not exists
    (
      select 1 from b
      where a.id=b.id and a.name=b.name
    )
      

  4.   

    要看你两个表的大小。select * from a where not exists (select 1 from b where a.id=b.id);
    select * from a where a.id not in(select distinct id from b);你可以查看一下该两条语句的执行计划,就可以看出那条语句的效高了。
    打开执行计划:set autotrace on;不过一般来说,如果外边表大,里边表小用in,反之用exists。
      

  5.   

    select * from a where not exists (select 1 from b where a.id=b.id) 
    select * from a where id not in (select id from b)