以下兩個查詢給了我相同的結果,但使用group by運行速度更快。這是否表明優化查詢將有利於在可能的情況下使用組?或者更可能是我遇到某種特殊情況?爲什麼羣組通過使sql查詢速度如此之快
更快的查詢(不太直觀我):
SELECT A.Advertiser
FROM (SELECT TOP (100) PERCENT Advertiser, [Final Status]
FROM dbo.Rehenas_View_2
GROUP BY [Final Status], Advertiser
HAVING ([Final Status] IS NULL)) AS A INNER JOIN
(SELECT TOP (100) PERCENT Advertiser, [Final Status]
FROM dbo.Rehenas_View_2 AS Rehenas_View_2_1
GROUP BY [Final Status], Advertiser
HAVING ([Final Status] = N'final')) AS B ON A.Advertiser = B.Advertiser
GROUP BY A.Advertiser
慢查詢(我正在努力簡化以上時,我注意到了速度差)
SELECT A.Advertiser
FROM (SELECT TOP (100) PERCENT Advertiser, [Final Status]
FROM dbo.Rehenas_View_2
WHERE ([Final Status] IS NULL)) AS A INNER JOIN
(SELECT TOP (100) PERCENT Advertiser, [Final Status]
FROM dbo.Rehenas_View_2 AS Rehenas_View_2_1
WHERE ([Final Status] = N'final')) AS B ON A.Advertiser = B.Advertiser
GROUP BY A.Advertiser
也許在兩者上運行EXPLAIN PLAN都會給你一個提示。 – duffymo 2010-12-07 04:44:47
性能將取決於這是哪個數據庫。當然看起來是MSSQL,但你應該用一個標籤明確地聲明它。 – 2010-12-07 04:50:05