SELECT TOP 20 LEFT(SUBSTRING(t.text, (s.statement_start_offset / 2) + 1, ((CASE s.statement_end_offset WHEN -1 THEN DATALENGTH(t.text) ELSE s.statement_end_offset END - s.statement_start_offset) / 2) + 1), 160) AS statement, s.execution_count AS calls, s.total_worker_time / 1000 AS total_cpu_ms, s.total_worker_time / s.execution_count / 1000 AS avg_cpu_ms, s.total_logical_reads AS logical_reads, s.last_execution_timeFROM sys.dm_exec_query_stats sCROSS APPLY sys.dm_exec_sql_text(s.sql_handle) tORDER BY s.total_worker_time DESCThis query is part of the "SQL Server health" pack: connect the database in DataRunner and install the whole pack at once, with queries and dashboard.
What the query shows
This query reads sys.dm_exec_query_stats, which keeps cumulative numbers for every plan in the plan cache, and returns the 20 statements with the most total CPU. It cuts the exact statement out of its batch using the start and end offsets, and shows the first 160 characters.
Run it when CPU is high, when the server is slow for everyone, or before buying a bigger machine. The top few statements often explain most of the load.
How to read each column
- statement: the text of the statement, truncated to 160 characters.
- calls: execution_count, how many times the plan ran since it entered the cache.
- total_cpu_ms: total_worker_time divided by 1000. The DMV stores microseconds, so this is milliseconds of CPU.
- avg_cpu_ms: total CPU divided by calls.
- logical_reads: total pages read from memory, 8 KB each. High reads usually go together with high CPU.
- last_execution_time: when it last ran. An old date means the statement is not a problem today.
Healthy and worrying results
There is no universal healthy number, because the list is always sorted from the worst. What matters is the shape. A flat list where no statement stands out is fine. A single statement taking a large share of all CPU, say a third or more as a rule of thumb, is the first thing to tune.
Read calls and avg_cpu_ms together. A few calls with a huge average points to one heavy report. Millions of calls with a tiny average points to a chatty application, and the fix is in the code that calls it.
What to do about it
Take the worst statement, run it with the actual execution plan on, and measure reads and time.
SET STATISTICS IO, TIME ON;To get the plan of a statement already in the cache, use its plan handle. Filter by a piece of the text.
SELECT s.execution_count, s.total_worker_time / 1000 AS total_cpu_ms, p.query_planFROM sys.dm_exec_query_stats sCROSS APPLY sys.dm_exec_sql_text(s.sql_handle) tCROSS APPLY sys.dm_exec_query_plan(s.plan_handle) pWHERE t.text LIKE '%part of the statement%';The usual suspects are a table scan where an index would do (see the missing indexes template), a function wrapped around a column in the WHERE clause, stale statistics, and a plan that is good for one parameter value and bad for another. Fix one thing, measure again, and move to the next.
Limits to know
- Only plans still in the cache appear. A restart, memory pressure or a recompile removes the numbers, so heavy statements can be missing.
- Counters start when the plan is cached, so a new statement may rank below an old one that has had more time to add up.
- Total CPU favors cheap queries that run very often. Sort by average CPU as well to find the heavy ones.
- The same text can show up more than once, one row per cached plan.
- The DMV needs VIEW SERVER STATE (VIEW SERVER PERFORMANCE STATE on SQL Server 2022 and later). Query Store, when enabled, keeps history across restarts.
Run it on a schedule in DataRunner
Because the cache is volatile, a daily copy keeps evidence you would otherwise lose. Schedule the query in DataRunner and send it by e-mail, Slack, Telegram, Discord, WhatsApp (beta), a signed webhook or Google Sheets, in CSV, XLSX, PDF or HTML. Comparing two days is the quickest way to see a new heavy statement after a release.