1. Find out the 10 SQL with the longest execution time (applicable to SQL SERVER 2005 and above)
SELECT top 10 (total_elapsed_time / execution_count)/1000 N 'average time ms' ,total_elapsed_time/1000 N'total time spent in ms' ,total_worker_time/1000 N'total CPU time used ms' ,total_physical_reads N 'total number of physical reads' ,total_logical_reads/execution_count N'number of logical reads per time' ,total_logical_reads N 'total number of logical reads' ,total_logical_writes N'total number of logical writes' ,execution_count N'execution times' ,creation_time N'statement compile time' ,last_execution_time N'last execution time' ,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 ) N 'execute statement' ,qp.query_plan FROM sys.dm_exec_query_stats AS qs CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) st CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) qp WHERE 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 ) not like '%fetch%' ORDER BY total_elapsed_time / execution_count DESC; If you want to filter on SQL, you can use Not like '%fetch%' can be replaced by like '%user%' to find the SQL containing the user keyword in the SQL statement
2 Find out the slowest executing SQL statement (applicable to SQL SERVER 2005 and above)
SELECT (total_elapsed_time / execution_count)/1000 N 'average time ms' ,total_elapsed_time/1000 N'total time spent in ms' ,total_worker_time/1000 N'total CPU time used ms' ,total_physical_reads N 'total number of physical reads' ,total_logical_reads/execution_count N'number of logical reads per time' ,total_logical_reads N 'total number of logical reads' ,total_logical_writes N'total number of logical writes' ,execution_count N'execution times' ,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) N'execute statement' ,creation_time N'statement compile time' ,last_execution_time N'last execution time' FROM sys.dm_exec_query_stats AS qs CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) st WHERE 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) not like 'fetch%' ORDER BY total_elapsed_time / execution_count DESC;
3 Find the most time-consuming top N T-SQL statements (applicable to SQL SERVER 2005 and above)
-- Assign an initial value of 30 to N declare @n int set @n=30 ;with maco as ( select top (@n) plan_handle, sum(total_worker_time) as total_worker_time , sum(execution_count) as execution_count , count(1) as sql_count from sys.dm_exec_query_stats group by plan_handle order by sum(total_worker_time) desc ) select t.text , a.total_worker_time , a.execution_count , a.sql_count from maco a cross apply sys.dm_exec_sql_text(plan_handle) t /* The result format is as follows text total_worker_time execution_count sql_count -------- ------------------ ----------------- --------- The content is omitted */
4 Top SQL with the most average CPU consumption (SQL SERVER 2005 or above)
SELECT TOP 5 total_worker_time / execution_count AS [Avg CPU Time], SUBSTRING(st.text, (qs.statement_start_offset/2)+1, ((CASE qs.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 ORDER BY total_worker_time/execution_count DESC
5 Top SQL that consumes the most CPU on average (SQL SERVER 2008 or above)
SELECT TOP 20 total_worker_time/1000 AS [total CPU consumption (ms)], execution_count [run times], qs.total_worker_time/qs.execution_count/1000 AS [average CPU time (ms)], last_execution_time AS [last execution time], min_worker_time /1000 AS [minimum execution time (ms)], max_worker_time /1000 AS [Maximum execution time (ms)], SUBSTRING(qt.text,qs.statement_start_offset/2+1, (CASE WHEN qs.statement_end_offset = -1 THEN DATALENGTH(qt.text) ELSE qs.statement_end_offset END -qs.statement_start_offset)/2 + 1) AS [using CPU syntax], qt.text [full syntax], qt.dbid, dbname=db_name(qt.dbid), qt.objectid,object_name(qt.objectid,qt.dbid) ObjectName FROM sys.dm_exec_query_stats qs WITH(nolock) CROSS apply sys.dm_exec_sql_text(qs.sql_handle) AS qt WHERE execution_count>1 ORDER BY (qs.total_worker_time/qs.execution_count/1000) DESC
6 Top SQL with the most CPU consumption (SQL SERVER 2008 or above)
SELECT TOP 20 total_worker_time/1000 AS [total CPU consumption (ms)], execution_count [run times], qs.total_worker_time/qs.execution_count/1000 AS [average CPU time (ms)], last_execution_time AS [last execution time], max_worker_time /1000 AS [maximum execution time (ms)], SUBSTRING(qt.text,qs.statement_start_offset/2+1, (CASE WHEN qs.statement_end_offset = -1 THEN DATALENGTH(qt.text) ELSE qs.statement_end_offset END -qs.statement_start_offset)/2 + 1) AS [using CPU syntax], qt.text [full syntax], qt.dbid, dbname=db_name(qt.dbid), qt.objectid,object_name(qt.objectid,qt.dbid) ObjectName FROM sys.dm_exec_query_stats qs WITH(nolock) CROSS apply sys.dm_exec_sql_text(qs.sql_handle) AS qt WHERE execution_count>1 ORDER BY total_worker_time DESC