用什么方法可以提高Not in的效率?

zxcht 2007-01-19 01:19:39
分别有两个表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?
...全文
441 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
jdsnhan 2007-01-22
  • 打赏
  • 举报
回复
a.key_id=b.key_id(+)
and b.key_id is null
是最快的方法
datacodecat 2007-01-21
  • 打赏
  • 举报
回复
不好意思提示写错了,记住了。只有在数据量很大的时候才使用提示,这样才可以邮箱的改变执行计划。
select /*+ use_hash(a,b)*/
b.*
from a,b
where a.key_id=b.key_id(+)
and b.key_id is null
datacodecat 2007-01-21
  • 打赏
  • 举报
回复

select /*+ hash_join (a,b)*/
b.*
from a,b
where a.key_id=b.key_id(+)
and b.key_id is null
qiyousyc 2007-01-19
  • 打赏
  • 举报
回复
更改为exits速度会快上很多的。
最好建立索引
lee_billiy 2007-01-19
  • 打赏
  • 举报
回复
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列上要有索引,这样执行的速度才会快
intotheheart 2007-01-19
  • 打赏
  • 举报
回复
select B.* from B
minus
select B.* from A,B where A.keyid=B.keyid
tgm78 2007-01-19
  • 打赏
  • 举报
回复
不行,key_id都加上索引或者主键
tgm78 2007-01-19
  • 打赏
  • 举报
回复
select B.* from A,B where not exists (
select A.key_id from A where A.key_id=B.key_id);

17,086

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧