我接觸伊茨克和他親切與下面的電子郵件回覆 - 這是正確的:
傑森您好,
我不知道你的查詢工作不適合是什麼意思您。 首先,您的查詢似乎是我在書中提供的修改版本。下面是書中的一個,和它的作品對我蠻好:
WITH Waits AS
(
SELECT
wait_type,
wait_time_ms/1000. AS wait_time_s,
100. * wait_time_ms/SUM(wait_time_ms) OVER() AS pct,
ROW_NUMBER() OVER(ORDER BY wait_time_ms DESC) AS rn,
100. * signal_wait_time_ms/wait_time_ms as signal_pct
FROM sys.dm_os_wait_stats
WHERE wait_time_ms > 0
AND wait_type NOT LIKE N'%SLEEP%'
)
SELECT
W1.wait_type,
CAST(W1.wait_time_s AS NUMERIC(12, 2)) AS wait_time_s,
CAST(W1.pct AS NUMERIC(5, 2)) AS pct,
CAST(SUM(W2.pct) AS NUMERIC(5, 2)) AS running_pct,
CAST(W1.signal_pct AS NUMERIC(5, 2)) AS signal_pct
FROM Waits AS W1
JOIN Waits AS W2
ON W2.rn <= W1.rn
GROUP BY W1.rn, W1.wait_type, W1.wait_time_s, W1.pct, W1.signal_pct
HAVING SUM(W2.pct) - W1.pct < 90 -- percentage threshold
OR W1.rn <= 5
ORDER BY W1.rn;
GO
至於你的版本,似乎是放錯位置的行註釋符(兩個破折號)右後AS PCT(請參見下面的高亮部分):
WITH Waits AS
(
SELECT wait_type,
wait_time_s = wait_time_ms/1000.,
pct = 100. * wait_time_ms/SUM(wait_time_ms) OVER(),
rn = ROW_NUMBER() OVER(ORDER BY wait_time_ms DESC)
FROM sys.dm_os_wait_stats o
WHERE wait_type NOT LIKE '%SLEEP%'
-- filter out additional irrelevant waits
)
SELECT W1.wait_type,
CAST(W1.wait_time_s AS DECIMAL(12, 2)) AS wait_time_s,
CAST(W1.pct AS DECIMAL(12, 2)) AS pct--,
CAST(SUM(W2.pct) AS DECIMAL(12, 2)) AS running_pct --<<XXX
FROM Waits AS W1
JOIN Waits AS W2 --<<XXX
ON W2.rn <= W1.rn --<<XXX
GROUP BY W1.rn, W1.wait_type, W1.wait_time_s, W1.pct
HAVING SUM(W2.pct) - W1.pct < 90 -- percentage threshold --<<XXX
ORDER BY W1.rn;
一旦我將其刪除,查詢看起來運行良好:
WITH Waits AS
(
SELECT wait_type,
wait_time_s = wait_time_ms/1000.,
pct = 100. * wait_time_ms/SUM(wait_time_ms) OVER(),
rn = ROW_NUMBER() OVER(ORDER BY wait_time_ms DESC)
FROM sys.dm_os_wait_stats o
WHERE wait_type NOT LIKE '%SLEEP%'
-- filter out additional irrelevant waits
)
SELECT W1.wait_type,
CAST(W1.wait_time_s AS DECIMAL(12, 2)) AS wait_time_s,
CAST(W1.pct AS DECIMAL(12, 2)) AS pct,
CAST(SUM(W2.pct) AS DECIMAL(12, 2)) AS running_pct --<<XXX
FROM Waits AS W1
JOIN Waits AS W2 --<<XXX
ON W2.rn <= W1.rn --<<XXX
GROUP BY W1.rn, W1.wait_type, W1.wait_time_s, W1.pct
HAVING SUM(W2.pct) - W1.pct < 90 -- percentage threshold --<<XXX
ORDER BY W1.rn;
如果它仍然不適合你,讓我知道你得到了什麼錯誤。
乾杯, 伊茨克
看來你是對的。當GROUP BY W1.run不在查詢輸出中時有什麼用處? – shahkalpesh
@shahkalpesh在一個明顯的修復任何答案? – whytheq