通过连接列和分页进行sql排序非常慢

时间:2015-03-20 07:36:37

标签: sql sql-server

我有疑问:

WITH O AS ( SELECT ROW_NUMBER() OVER(ORDER BY Advertisers.advertiser_title asc) as 'num', ROW_NUMBER() OVER(ORDER BY spot_date asc) as 'num2',product_title,advertiser_title,brand_title,sector_title,channel_title,copy_title,prog_title,clipType_title,Spots_tv.spot_id,Spots_tv.spot_date,Spots_tv.time_start,Spots_tv.time_end,Spots_tv.clipppos_id,Spots_tv.prog_id,Spots_tv.channel_id,Spots_tv.copy_id,Spots_tv.duration,official_cost FROM Spots_tv 
RIGHT JOIN Copies_tv ON Copies_tv.copy_id = Spots_tv.copy_id 
RIGHT JOIN ClipPositions ON ClipPositions.clippos_id = Spots_tv.clipppos_id 
RIGHT JOIN Programmes ON Programmes.prog_id = Spots_tv.prog_id 
RIGHT JOIN Channels ON Channels.channel_id = Spots_tv.channel_id 
RIGHT JOIN Products ON Products.product_id = Copies_tv.product_id 
RIGHT JOIN ClipTypes ON ClipTypes.cliptype_id = Copies_tv.cliptype_id 
RIGHT JOIN Advertisers ON Advertisers.advertiser_id = Products.advertiser_id 
RIGHT JOIN Brands ON Brands.brand_id = Products.brand_id 
RIGHT JOIN Classes ON Classes.class_id = Products.class_id 
RIGHT JOIN Producers ON Producers.producer_id = Brands.producer_id 
RIGHT JOIN Categories ON Categories.category_id = Classes.category_id 
RIGHT JOIN Sectors ON Sectors.sector_id = Categories.sector_id
WHERE Advertisers.advertiser_id IN (1640311,1640315,1640330) AND Spots_tv.spot_date BETWEEN '2014-3-20' AND '2015-3-20' ) 

SELECT O.spot_id,O.channel_id,O.copy_id,O.duration,O.official_cost,product_title,O.spot_date,advertiser_title,brand_title,sector_title,channel_title,copy_title,prog_title,clipType_title,O.time_start,O.time_end FROM O 
WHERE O.num2 BETWEEN 1 AND 15 ORDER BY O.num2 ASC

子查询,它工作得非常快,子查询中有11000多行,很快应该有超过百万行,完整查询需要不到一秒,但如果我改变

WHERE O.num2 BETWEEN 1 AND 15 ORDER BY O.num2 ASC

WHERE O.num BETWEEN 1 AND 15 ORDER BY O.num ASC

它需要超过14秒,为什么会有这么大的差异?我知道按字符串排序速度较慢但是,我该怎么做才能加快这个查询的速度呢?

抱歉英语不好

0 个答案:

没有答案