dataiotech Labs
Scripts Library/sqlserver/#58
Script #58
sqlserver
Performanceread-onlyMin Version: 2008

SQL Server Plan Cache Bloat from Single-Use Ad-hoc Plans

Shows the number of single-use plans and the memory they take. Used to decide on 'optimize for ad hoc workloads'.

#plan-cache#ad-hoc#optimize-for-ad-hoc
sqlserver · sql
SELECT objtype,
       COUNT(*) AS plans,
       SUM(CASE WHEN usecounts = 1 THEN 1 ELSE 0 END) AS single_use_plans,
       CAST(SUM(CAST(size_in_bytes AS BIGINT)) / 1048576.0 AS DECIMAL(18,1)) AS total_mb,
       CAST(SUM(CASE WHEN usecounts = 1 THEN CAST(size_in_bytes AS BIGINT) ELSE 0 END) / 1048576.0 AS DECIMAL(18,1)) AS single_use_mb
FROM sys.dm_exec_cached_plans
GROUP BY objtype
ORDER BY total_mb DESC;
Operational Notes & Remediation Guidance

Verify in a staging environment prior to running in production. This is a read-only diagnostic query; it does not acquire exclusive locks.

Production Safety & Execution Standards
  • This script is designed for read-only diagnostics; it does not alter schema or modify data directly.
  • Prior to executing on production systems under high load, verify the target database version (Min. Version: 2008).
  • Exercise caution with mutating actions (e.g., terminating sessions or dropping indexes) referenced in the notes section.

Engineering Triad: Related Runbooks & Incident Response

Which production incidents and emergency drills execute this script as a diagnosis step?

Runbook Library (63)
Related Alerts & Metrics
M25SQL Server Page Life Expectancy (PLE)SQL Server
M35SQL Server Compilations per Batch RequestSQL Server