1

这是取自 Itzik Ben-Gan 的书“T-SQL Querying”的查询,但似乎不适用于我的 2012 db - 尽管我认为脚本中一定有错字:

-- Isolate top waits
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;
GO

如果我注释掉标记的部分,XXX那么它会返回我期望的结果:

在此处输入图像描述

如果我运行整个查询,则会导致以下内容重复且列不正确running_pct

在此处输入图像描述

我该如何解决?我尝试添加AND W2.wait_type <> W1.wait_typeON子句中 - 这有帮助,但似乎随后丢弃wait_typeCXPACKET


编辑

他应该W1.rn在那个GROUP BY部分吗?

4

1 回答 1

0

我联系了 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;

如果它仍然无法为您运行,请告诉我您遇到的错误是什么。

干杯,伊齐克

于 2013-09-03T06:57:00.633 回答