Skip to content

Instantly share code, notes, and snippets.

@bartread
Last active January 26, 2017 10:31
Show Gist options
  • Save bartread/078c10b1249bc30fd4022eaea8e6ece5 to your computer and use it in GitHub Desktop.
Save bartread/078c10b1249bc30fd4022eaea8e6ece5 to your computer and use it in GitHub Desktop.
Get most expensive t-SQL queries in terms of I/O or CPU usage
USE [YOUR_DATABASE_NAME]; -- Substitute for your database name
GO
SELECT TOP 10
t.text ,
execution_count ,
statement_start_offset AS stmt_start_offset ,
s.sql_handle ,
s.plan_handle ,
s.total_worker_time / 1000 AS total_cpu_time_millis,
s.total_worker_time / s.execution_count / 1000 AS avg_cpu_time_millis ,
s.total_logical_reads ,
s.total_logical_reads / s.execution_count AS avg_logical_reads ,
s.total_logical_writes ,
s.total_logical_writes / s.execution_count AS avg_logical_writes ,
s.total_physical_reads ,
s.total_physical_reads / s.execution_count AS avg_physical_reads,
s.execution_count ,
s.last_execution_time ,
s.last_elapsed_time / 1000 AS last_elapsed_time_millis,
s.last_logical_reads,
s.last_physical_reads,
s.last_worker_time / 1000 AS last_worker_time_millis
FROM sys.dm_exec_query_stats AS s
CROSS APPLY sys.dm_exec_sql_text(s.sql_handle) AS t
WHERE DB_NAME(t.dbid) = 'YOUR_DATABASE_NAME' -- Substitute for your database name
ORDER BY -- Comment/uncomment below to order by column of interest
avg_logical_reads DESC
-- avg_physical_reads DESC
-- avg_cpu_time_millis DESC
-- last_elapsed_time_millis DESC
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment