关于Exists的效率问题

princeml 2006-04-26 05:31:54
论坛里有个帖子,原贴地址:
http://community.csdn.net/Expert/topic/4192/4192302.xml?temp=.9121973
帖子内容:
查询订购的产品至少包含了订单3号中所订购产品的订单。
Select distinct order_no from sale_item a
Where order_no<>'3'and not exists (
Select * from sale_item b where order_no ='3' and not exists
(select * from sale_item c where c.order_no=a.order_no and c.prod_id=b.prod_id))

我想讨论的是,当sale_item中的数据量大于10000时,查询的时间非常长,运行了20分钟都还没有完。这种效率在实际应用中肯定是不行的,有没有提高效率的方法呢?
...全文
151 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Yang_ 2006-04-26
  • 打赏
  • 举报
回复
当sale_item中的数据量大于10000时,查询的时间非常长,运行了20分钟都还没有完。

没有索引吧?
在(order_no,prod_id)建索引试试

panjinfu80 2006-04-26
  • 打赏
  • 举报
回复
exists (
Select 1 from sale_item b where order_no ='3' and not exists
(select 1 from sale_item c where c.order_no=a.order_no and c.prod_id=b.prod_id))
子陌红尘 2006-04-26
  • 打赏
  • 举报
回复
从SQL语句上理解,似乎是要查找出包含订单"3"所有订购产品的订单,不知道楼主的需求是否是这样。
WangZWang 2006-04-26
  • 打赏
  • 举报
回复
--LZ的语句应该等价于
Select distinct order_no from (
Select order_no from sale_item
Where order_no>'3'
union
Select order_no from sale_item
Where order_no<'3') as a
lovcal 2006-04-26
  • 打赏
  • 举报
回复
不知道是不是搂住的意思
能把你的sale_item字段列出来一下么?
lovcal 2006-04-26
  • 打赏
  • 举报
回复
Select distinct order_no from sale_item where prod_id in (select prod_id from sale_item where order_no ='3') and (order_no >'3' or order_no <'3')

这样呢?

34,594

社区成员

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

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