存在性判断如何写更有效率

winstar66 2005-06-05 11:11:30
--存在性判断如何写更有效率
--表a与表b都有item_code字段,表b有数百万条数据,表a每次几十到数百条不等,
--如何有效率的方式从表a中读取不存在于表b的数据
--写了下面两种,还有没有其它的,关键是要执行效率
1:select a.* from a where a.item_code not in (select item_code from b)
2:select a.* from a where not exists(select * from b where b.item_code = a.item_code)
...全文
100 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
posonhuang 2005-06-06
  • 打赏
  • 举报
回复
in 会快点,这个在以前的帖子中已经 讨论过了.
lh1979 2005-06-06
  • 打赏
  • 举报
回复
select a.* from a where not exists(select 1 from b where b.item_code = a.item_code)


呵呵,建索引
还有把子句中的*改为数字的话,更快
lengxiaowei 2005-06-06
  • 打赏
  • 举报
回复
b.item_code上面建立索引,应该会更快一些
lengxiaowei 2005-06-06
  • 打赏
  • 举报
回复
select a.* from a where not exists(select * from b where b.item_code = a.item_code)


这个效率比较高一些
winstar66 2005-06-06
  • 打赏
  • 举报
回复
第2个速度要比第一个快很多,有没有更好的方法,请各位指点~~~~~~
gdczch 2005-06-05
  • 打赏
  • 举报
回复
我觉得上面两种的速度都会差不多,
要不这样

select a.* from a , b where a.col >= '范围' and a.id = b.id

说错了不要骂啊

34,873

社区成员

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

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