sql查询遇到的怪问题

goodluckalong 2009-07-07 04:53:28
加精
问题描述:
有两个表:产品分类表(81条记录)及产品表(7016条记录)
单独对各个表进行各种查询,都一切正常
执行如下查询语句时(排序条件属于产品表)

执行
select top 12 t1.* from (select * from product) t1 where id not in (select top 36 id from (select * from product) t2 where 1=1 and lang=1 and isvip=0 order by isvip desc,colorBoxL asc,ctime desc) and 1=1 and lang=1 and isvip=0 order by isvip desc,colorBoxL asc,ctime desc
花了一分多钟才得出结果
删去任何一处的order by 子句( order by isvip desc,colorBoxL asc,ctime desc)
即执行
select top 12 t1.* from (select * from product) t1 where id not in (select top 36 id from (select * from product) t2 where 1=1 and lang=1 and isvip=0 order by isvip desc,colorBoxL asc,ctime desc) and 1=1 and lang=1 and isvip=0

select top 12 t1.* from (select * from product) t1 where id not in (select top 36 id from (select * from product) t2 where 1=1 and lang=1 and isvip=0 ) and 1=1 and lang=1 and isvip=0 order by isvip desc,colorBoxL asc,ctime desc
均正常,马上得出结果

把数据导入同一台机子上的其它数据库,再执行相同的查询,马上得出结果,即是机子也不存在性能不足的问题.
请问有知道原因的么,及问题是怎么会产生的?


...全文
2182 113 打赏 收藏 转发到动态 举报
写回复
用AI写文章
113 条回复
切换为时间正序
请发表友善的回复…
发表回复
wushuang3723 2009-07-18
  • 打赏
  • 举报
回复
order by 是一项最费时的操作,尤其实在没有建索引的情况下,然后又加上使用了in操作,两个查询产生了一个庞大的笛卡尔积,建议使用 exists
仙茅 2009-07-18
  • 打赏
  • 举报
回复
太复杂了,唔唔看不懂!
dxl0501 2009-07-17
  • 打赏
  • 举报
回复
关注,学习各位大虾的方法
dxl0501 2009-07-17
  • 打赏
  • 举报
回复
关注,学习各位大虾的方法
江紫枫 2009-07-17
  • 打赏
  • 举报
回复
有点迷糊~~~关注
panda426 2009-07-17
  • 打赏
  • 举报
回复
select top 12 t1.* from
product as t1 where id not in (select top 36 id from product as t2 where 1=1 and lang=1 and isvip=0 group by id order by isvip desc,colorBoxL asc,ctime desc) and 1=1 and lang=1 and isvip=0 order by isvip desc,colorBoxL asc,ctime desc
不熟悉你们的数据结构
随便写写,拿去试试看
微醺_zZ 2009-07-16
  • 打赏
  • 举报
回复
去掉任意一个Order By不都不影响结果么?
关注一下,期待一个完美的解释
gentlebrother 2009-07-13
  • 打赏
  • 举报
回复
mark
xiejunsheng888 2009-07-13
  • 打赏
  • 举报
回复
瞎逛
goodluckalong 2009-07-13
  • 打赏
  • 举报
回复
[Quote=引用 69 楼 small_well 的回复:]
试了一下sheepcyk的方法: 
  UPDATE STATISTICS Product
  WITH fullscan
  UPDATE STATISTICS ProductSort
  WITH fullscan
果然是这样的!佩服万分!
回家面壁去!多看书,多学习!
[/Quote]
以下省去十万字(感激、佩服的话),
......
超维电脑科技 2009-07-12
  • 打赏
  • 举报
回复
关注
丁码农 2009-07-12
  • 打赏
  • 举报
回复
是sql2005的还是sql2000数据库?代码有点复杂,仔细研究一下。
alexanda2000 2009-07-11
  • 打赏
  • 举报
回复
向sheepcyk学习!
wuguanlin 2009-07-11
  • 打赏
  • 举报
回复
对排序字段建索引,这个很有必要的。
Tomzzu 2009-07-11
  • 打赏
  • 举报
回复
1. not in本来效率就不高
2. 整个查询语句要排序, 占用资源
3. not in后面的那个子查询语句里面排序是罪魁祸首, 结果集越多查询就越慢, 并且在那个里面的排序完全没有必要
AuC 2009-07-11
  • 打赏
  • 举报
回复
学习了
-晴天 2009-07-11
  • 打赏
  • 举报
回复
估计是锁方面的问题.
有个疑问,既然在 where 子句中有了 isVip=0,是否没必要再对 isVip 来排序呢?
另外,楼主建的索引貌似也不对,似乎不应该建复合索引,而应对每个列单独建索引.
改用下面的语句,结果相同,运行时间不到1秒:
select top 12 Product.ID 
into #T
from Product inner join ProductSort on ProductSort.SID=Product.SID
where lang=1 and isVip=0
order by ctime desc

select top 12 t.* from (
select Product.ID,Product.SID,Product.colorBoxL,smallimageurl,htmlurl,imageurl,Product.name,Product.nameEn,
isCreate,readcount,number,UnitCost,rebateCost,ProductSort.url,ProductSort.dir,ctime,video,hasCost,isVip,lang
from Product inner join ProductSort on ProductSort.SID=Product.SID
where 1=1 and lang=1 and isVip=0
) T
where T.id not in (select id from #T)
order by ctime desc

drop table #T
sundl2268 2009-07-11
  • 打赏
  • 举报
回复
进来学习一下!
zhengyuqi281 2009-07-11
  • 打赏
  • 举报
回复
sql 语句的形式,可能让系统,,,
applesclock 2009-07-11
  • 打赏
  • 举报
回复
我很想要啊、
加载更多回复(92)

22,207

社区成员

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

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