2013-08-29 111 views
1

這是一個來自Itzik Ben-Gan的書「T-SQL查詢」的查詢,但似乎沒有針對我的2012分貝 - 儘管我認爲腳本中必須存在拼寫錯誤:運行總計不工作

-- 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然後返回結果,我期望:

enter image description here

如果我運行它導致與重複以下和不正確的列running_pct整個查詢:

enter image description here

我該如何解決這個問題? 我嘗試添加AND W2.wait_type <> W1.wait_typeON條款 - 這有助於但似乎然後丟棄wait_typeCXPACKET


編輯

他應該在GROUP BY部分W1.rn

+0

看來你是對的。當GROUP BY W1.run不在查詢輸出中時有什麼用處? – shahkalpesh

+0

@shahkalpesh在一個明顯的修復任何答案? – whytheq

回答

0

我接觸伊茨克和他親切與下面的電子郵件回覆 - 這是正確的:

傑森您好,

我不知道你的查詢工作不適合是什麼意思您。 首先,您的查詢似乎是我在書中提供的修改版本。下面是書中的一個,和它的作品對我蠻好:

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; 

如果它仍然不適合你,讓我知道你得到了什麼錯誤。

乾杯, 伊茨克