Advertisement
bytecoded

Identifying Most Costly Queries by CPU

Oct 29th, 2019
481
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 0.91 KB | None | 0 0
  1. SELECT TOP 5
  2.     [Query] = [Query],
  3.     [CPU]   = SUM([CPU])
  4.  
  5. FROM (
  6.  
  7.     SELECT
  8.         [CPU]   = [Total CPU Used],
  9.         [Query] = dbo.fn_GetSQLNormalized([Parent Query])
  10.  
  11.     FROM
  12.     (
  13.         SELECT TOP 20
  14.             [Average CPU Used]  = total_worker_time / qs.execution_count,
  15.             [Total CPU Used]    = total_worker_time,
  16.             [Execution COUNT]   = qs.execution_count,
  17.             [Individual Query]  = SUBSTRING(
  18.                 qt.text,
  19.                 qs.statement_start_offset / 2,
  20.                 (CASE
  21.                     WHEN qs.statement_end_offset = -1
  22.                     THEN LEN(CONVERT(NVARCHAR(MAX), qt.text)) * 2
  23.                     ELSE qs.statement_end_offset
  24.                 END - qs.statement_start_offset) / 2
  25.             ),
  26.             [Parent Query]      = qt.text,
  27.             [DATABASE Name]     = DB_NAME(qt.dbid)
  28.  
  29.         FROM
  30.             sys.dm_exec_query_stats qs
  31.  
  32.         CROSS APPLY
  33.             sys.dm_exec_sql_text(qs.sql_handle) AS qt
  34.  
  35.         ORDER BY
  36.             [Average CPU Used] DESC
  37.  
  38.     ) AS Queries
  39.  
  40. ) AS NormalizedQueries
  41.  
  42. GROUP BY [Query]
  43.  
  44. ORDER BY [CPU] DESC
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement