Database Administration — Sun Mar 29

← Home | ← database-admin

MSSQL - DBA Toolkit - Top Queries by Resource

Sun Mar 29
#sql-server #dba #monitoring #troubleshooting #performance #query-tuning #tool-kit

Something is Using All the CPU. Find It.

The server is under pressure. CPU is high. Disk reads are spiking.

You don’t need to wait for a trace or a profiler session. The answer is already in sys.dm_exec_query_stats — SQL Server has been keeping score since the last restart.

This query pulls the top offenders by two dimensions:

  1. Total CPU — which queries have burned the most processor time overall
  2. Logical reads — which queries are hammering the buffer pool hardest

What This Query Does

sys.dm_exec_query_stats accumulates statistics for every cached query plan. Combined with sys.dm_exec_sql_text, you get the actual statement text alongside the numbers.

Key columns:

ColumnMeaning
Total CPU (sec)Cumulative CPU used by all executions
Avg CPU (ms)Per-execution cost — high avg means each run is expensive
Avg Logical ReadsBuffer pool reads per execution — drives memory pressure
Avg Physical ReadsReads that missed cache and hit disk
Avg Duration (ms)Wall-clock time per execution
Plan CompiledWhen this plan entered cache — old plans have more data

Stats reset when plans are evicted from cache (memory pressure, DBCC FREEPROCCACHE, or restart). Sort by Total CPU for cumulative impact, Avg CPU to find queries that are individually expensive regardless of frequency.

Top Queries by Resource Script

Top Queries by Resource.sql
    Loading…
  

Gareth Winterman