Skip to content

Instantly share code, notes, and snippets.

@revirth
Last active August 29, 2015 14:06
Show Gist options
  • Save revirth/6871f778d9c6c2f3c30f to your computer and use it in GitHub Desktop.
Save revirth/6871f778d9c6c2f3c30f to your computer and use it in GitHub Desktop.
-- http://msdn.microsoft.com/ko-kr/library/ms189741.aspx
SELECT TOP 20 query_stats.query_hash AS "Query Hash",
SUM(query_stats.total_worker_time) / SUM(query_stats.execution_count)/1000 AS "Avg CPU Time",
MIN(query_stats.statement_text) AS "Statement Text"
FROM
(SELECT QS.*,
SUBSTRING(ST.text, (QS.statement_start_offset/2) + 1,
((CASE statement_end_offset
WHEN -1 THEN DATALENGTH(ST.text)
ELSE QS.statement_end_offset END
- QS.statement_start_offset)/2) + 1) AS statement_text
FROM sys.dm_exec_query_stats AS QS
CROSS APPLY sys.dm_exec_sql_text(QS.sql_handle) as ST) as query_stats
GROUP BY query_stats.query_hash
ORDER BY 2 DESC;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment