Category Archives: MSSQLServer

Find top CPU consuming queries in SQL Server

Query return TOP 20 CPU consuming queries for checking performance in SQL Server

Query will return the result of TOP CPU consuming queries with other details: specifically, it will provide insights into the most resource-intensive processes, including execution time, frequency of execution, and associated user accounts, allowing for a comprehensive analysis of performance bottlenecks and facilitating informed decisions on optimization strategies that could significantly enhance overall system efficiency.

SELECT TOP 20 SUBSTRING(eqt.TEXT, (eqs.statement_start_offset / 2) + 1, (
(
CASE eqs.statement_end_offset
WHEN - 1
THEN DATALENGTH(eqt.TEXT)
ELSE eqs.statement_end_offset
END - eqs.statement_start_offset
) / 2
) + 1) Statement
,eqs.execution_count Total_number_of_execution
,eqs.total_worker_time
,eqs.last_worker_time
,eqs.total_elapsed_time / 1000000 Total_Duration_In_Sec
,eqs.last_elapsed_time / 1000000 Last_Duration_In_Sec
,eqs.last_execution_time
,eqs.total_logical_reads
,eqs.last_logical_reads
,eqs.total_logical_writes
,eqs.last_logical_writes
,eqp.query_plan
FROM sys.dm_exec_query_stats eqs
CROSS APPLY sys.dm_exec_query_plan(eqs.plan_handle) eqp
CROSS APPLY sys.dm_exec_sql_text(eqs.sql_handle) eqt
ORDER BY eqs.total_worker_time DESC