分别有两个表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?
例如 :
select B.* from A,B where B.key_id not in (
select A.key_id from A
)有其他高效的方法吗?外联?NOT Exist?
select A.key_id from A where A.key_id=B.key_id);
minus
select B.* from A,B where A.keyid=B.keyid
select 1 from A where A.key_id=B.key_id);
因为not in没办法使用到索引,而改成not exists后就可以,但有个前提:表A,B上的key_id列上要有索引,这样执行的速度才会快
最好建立索引
select /*+ hash_join (a,b)*/
b.*
from a,b
where a.key_id=b.key_id(+)
and b.key_id is null
select /*+ use_hash(a,b)*/
b.*
from a,b
where a.key_id=b.key_id(+)
and b.key_id is null
and b.key_id is null
是最快的方法