如何提高sql中 in /not in 操作的速度

格利高里 2000-12-27 07:03:00
大家都知道,sql语句的中 in /not in 操作是很慢的, 其中in 可转化为
等联结操作,但 not in 怎样转换才可以?我有两个表,结构基本一样,都至少
有几十万条记录,打算查出一个表中在另外一个表中不存在的记录。
每次我都这样做,
delete from a
from table1 a , table2 b
where a.key = b.key
然后看剩下的记录 :)

有没有高手知道直接查询的sql语句?
...全文
274 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
格利高里 2000-12-28
  • 打赏
  • 举报
回复
不错不错,有没有更好的? :)
xiaow 2000-12-28
  • 打赏
  • 举报
回复
可以用左连接:
select a.key from table1 a
left join table2 b
on a.key=b.key
where b.key is null
The_east_key 2000-12-28
  • 打赏
  • 举报
回复
请到http://www.etechbase.net/advsearch.php,将你这个问题输入到检索框,然后选择准确匹配查询,应该可以帮助你解决这个问题的。
zheng 2000-12-27
  • 打赏
  • 举报
回复
select a.empno from emp a where a.empno not in

(select empno from emp1 where job=’SALE’);


---- 倘若利用外部联接,改写命令如下:


select a.empno from emp a ,emp1 b

where a.empno=b.empno(+)

and b.empno is null

and b.job=’SALE’;


---- 可以发现,运行速度明显提高.


34,594

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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