分别有两个表A和B,表B中的记录包含表A中的记录,且比表A多。如何查找出表B中不存在于表A中的记录。用not in可以找出,但是效率太低了,如果涉及表A、表B记录量很多的话,更是跑不出来。
例如 :
select B.* from A,B where B.key_id not in (
select A.key_id from A
)有其他高效的方法吗?外联?NOT Exist?

解决方案 »

  1.   

    select B.* from A,B where not exists (
    select A.key_id from A where A.key_id=B.key_id);
      

  2.   

    不行,key_id都加上索引或者主键
      

  3.   

    select B.* from B
    minus
    select B.* from A,B where A.keyid=B.keyid
      

  4.   

    select B.* from B where not exists (
    select 1 from A where A.key_id=B.key_id); 
    因为not in没办法使用到索引,而改成not exists后就可以,但有个前提:表A,B上的key_id列上要有索引,这样执行的速度才会快
      

  5.   

    更改为exits速度会快上很多的。
    最好建立索引
      

  6.   


    select  /*+ hash_join (a,b)*/
     b.*
    from a,b
    where a.key_id=b.key_id(+)
          and b.key_id is null
      

  7.   

    不好意思提示写错了,记住了。只有在数据量很大的时候才使用提示,这样才可以邮箱的改变执行计划。
    select  /*+ use_hash(a,b)*/
     b.*
    from a,b
    where a.key_id=b.key_id(+)
          and b.key_id is null
      

  8.   

    a.key_id=b.key_id(+)
          and b.key_id is null
    是最快的方法