我联系了 Itzik,他回复了下面的电子邮件——这是正确的:
嗨杰森,
我不确定您所说的查询不适合您是什么意思。首先,您的查询似乎是我在书中提供的修改版本。这是书中的一个,它对我来说很好用:
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;
如果它仍然无法为您运行,请告诉我您遇到的错误是什么。
干杯,伊齐克