Find the most expensive stored procedures in SQL Server
Object-level stats for stored procedures: which routines burn the most server time, per call and in total.
The task
sys.dm_exec_procedure_stats aggregates elapsed time, CPU, logical reads and call counts per cached procedure plan: the same shape as sys.dm_exec_query_stats but rolled up to the procedure, so a 500-statement routine becomes one row. That is the level most tuning conversations actually happen at: this procedure got slower, why.
The snippet
Written for SQL Server.
SELECT TOP 25
OBJECT_SCHEMA_NAME(ps.object_id, ps.database_id) AS schema_name,
OBJECT_NAME(ps.object_id, ps.database_id) AS procedure_name,
ps.execution_count AS calls,
CAST(ps.total_elapsed_time / 1000.0 AS DECIMAL(18,1)) AS total_ms,
CAST(ps.total_elapsed_time / 1000.0 / NULLIF(ps.execution_count, 0) AS DECIMAL(18,2)) AS mean_ms,
CAST(ps.total_worker_time / 1000.0 / NULLIF(ps.execution_count, 0) AS DECIMAL(18,2)) AS mean_cpu_ms,
ps.last_execution_time
FROM sys.dm_exec_procedure_stats ps
WHERE ps.database_id = DB_ID()
AND ps.object_id IS NOT NULL
ORDER BY ps.total_elapsed_time DESC;
工作原理
total_elapsed_time is wall-clock across all executions since the plan was cached; dividing by execution_count gives mean time per call, which separates a fast procedure called a million times from a slow one called rarely; both need attention, for different reasons. mean_cpu_ms beside it shows whether the time is CPU-bound or mostly waiting (I/O, locks). The counters reset when the procedure's plan is evicted or the instance restarts; Query Store gives the durable version of this analysis for databases that need history. Triggers show up in sys.dm_exec_trigger_stats with the same columns.
Variations
Rank by average time (slow per call, not frequent)
SELECT TOP 25
OBJECT_SCHEMA_NAME(ps.object_id, ps.database_id) AS schema_name,
OBJECT_NAME(ps.object_id, ps.database_id) AS procedure_name,
ps.execution_count AS calls,
CAST(ps.total_elapsed_time / 1000.0 / NULLIF(ps.execution_count, 0) AS DECIMAL(18,2)) AS mean_ms
FROM sys.dm_exec_procedure_stats ps
WHERE ps.database_id = DB_ID()
AND ps.execution_count > 10
ORDER BY mean_ms DESC;
Questions that come up
A procedure is slow per call. What next?
Get inside it: sys.dm_exec_query_stats per statement (matched via the procedure's plan handle) or Query Store's per-query view shows which statement inside the routine owns the time. Procedure-level stats say where to look; statement-level stats say what to fix.
Why do counts reset after a deploy?
Altering a procedure recompiles and replaces its cached plan, which resets its counters, so a deploy makes every touched procedure look freshly cheap. Check last_execution_time and correlate with deployment times before concluding a regression or a fix; Query Store survives this because its stats live in the database, not the cache.
Keep reading
Run these with autocomplete that knows your tables
SQLly is a native SQL workbench for macOS, Windows and Linux. Paste a snippet into a live connection and you get schema-aware IntelliSense, a plan viewer and guards against unsafe edits.