sql嵌套查询有bug吗?

xjchen 2006-09-06 05:27:49
select top 40 * from view_sell_search where xinghao like 'rt9161%' and
id not in(select top 40 id from view_sell_search where xinghao like 'aaaaa%' order by grade desc,flashtime desc)
order by grade desc,flashtime desc

一段分页 采用id not in 的方法,可发现查询运行死在那里出不来了.
但如果把最先40条的id 直接写入 not in 里马上可以出来. 运行
select top 40 id from view_sell_search where xinghao like 'aaaaa%' order by grade desc,flashtime desc 也很快. 怎么回事?
另外 aaaaa如果换成其他的如bbbbbb 也能很快出来,而且符合bbbbbb的记录还要更多.


...全文
116 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
zicxc 2006-09-06
  • 打赏
  • 举报
回复
可以用临时表
select top 40 id into #t from view_sell_search where xinghao like 'aaaaa%' order by grade desc,flashtime desc

select top 40 * from view_sell_search where xinghao like 'rt9161%' and
id not in(select id from #t)
order by grade desc,flashtime desc
zicxc 2006-09-06
  • 打赏
  • 举报
回复
view_sell_search 是视图吧

sql试图优化,但是在考虑视图,连接条件等进行的优化的结果效率很成问题

估计很难改

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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