Skip to main content

Show the most costful queries in T-SQL.

select TOP 50
    SUBSTRING(qt.TEXT, (qs.statement_start_offset/2)+1,
    ((case qs.statement_end_offset
    when -1 then DATALENGTH(qt.TEXT)
    else qs.statement_end_offset
    end - qs.statement_start_offset)/2)+1),
    qs.execution_count,
    qs.total_logical_reads, qs.last_logical_reads,
    qs.total_logical_writes, qs.last_logical_writes,
    qs.total_worker_time,
    qs.last_worker_time,
    qs.total_elapsed_time/1000000 total_elapsed_time_in_S,
    qs.last_elapsed_time/1000000 last_elapsed_time_in_S,
    qs.last_execution_time,
    qp.query_plan
from sys.dm_exec_query_stats qs
cross APPLY sys.dm_exec_sql_text(qs.sql_handle) qt
cross APPLY sys.dm_exec_query_plan(qs.plan_handle) qp
order by qs.total_logical_reads desc -- logical reads
-- ORDER BY qs.total_logical_writes DESC -- logical writes
-- ORDER BY qs.total_worker_time DESC -- CPU time