Statistics Maintenance
Automated stale statistics detection and update. Identifies statistics with high modification counts relative to table size and runs targeted UPDATE STATISTICS. Processes databases individually to avoid silent failures.
Inspired by Ola Hallengren's SQL Server Maintenance Solution, with a focus on surgical precision — instead of updating everything on a schedule, it identifies genuinely stale statistics based on modification rates and table size, then updates only those.
Key proc: optimize.sp_StatisticsMaintenance
How It Works
The proc loops through each database individually and identifies stale statistics based on these criteria:
| Condition | Threshold | Rationale |
|---|---|---|
| No modifications + old | > 30 days since update | Stats may be outdated even without changes |
| Has modifications + aging | > 7 days since update | Allow batching rather than constant updates |
| Large tables with mods | > 5% modification rate | High-row tables need tighter thresholds |
| Small tables with mods | > 20% modification rate | Small tables tolerate more churn |
Exclusions: CDC schema, empty tables, system tables.
Parameters
| Parameter | Default | Description |
|---|---|---|
@Databases | USER_DATABASES | Select databases using keywords, wildcards, and exclusions. See syntax |
@Run | 1 | 1 = execute updates and log to history table (default). 0 = dry run — display stale statistics without updating, useful for previewing what would change |
@StatisticsSample | NULL | NULL = SQL Server default. 100 = FULLSCAN. 1–99.99 = sample percentage |
@TableName | NULL | NULL = all tables. Supports CSV with optional schema prefix (dbo.MyTable) |
@DetailOutput | 0 | 0 = minimal output (database progress only). 1 = detailed results per statistic |
@PurgeAfterDays | 90 | Purge history records older than this many days |
Logging
Every update is logged to optimize.StatisticsMaintenanceHistory:
| Column | Description |
|---|---|
| Database, Schema, Table, StatisticName | Identifies the specific statistic updated |
| LastUpdated | When the stat was last updated before this run |
| DaysSinceUpdate | How stale the stat was |
| ModificationCount | Number of row modifications since last update |
| RowCount, ModificationPct | Table size and modification rate |
| Outcome | Success or Failure |
| Run | Groups updates from a single execution |
History is automatically purged based on @PurgeAfterDays (default 90 days).
Usage
Basic
-- Execute updates on all user databases (default)
EXEC optimize.sp_StatisticsMaintenance;
-- Dry run: see what would be updated without executing
EXEC optimize.sp_StatisticsMaintenance @Run = 0;
-- Single database with verbose output
EXEC optimize.sp_StatisticsMaintenance
@Databases = 'MyDatabase',
@DetailOutput = 1;
-- MC databases only (wildcard)
EXEC optimize.sp_StatisticsMaintenance @Databases = 'mc_%';
-- All user databases except one
EXEC optimize.sp_StatisticsMaintenance @Databases = 'USER_DATABASES, -mc_largedb';
Full Scan on Specific Table
EXEC optimize.sp_StatisticsMaintenance
@Databases = 'MyDatabase',
@TableName = 'dbo.LargeTable',
@StatisticsSample = 100;
Check the Log
-- Recent updates
SELECT * FROM optimize.StatisticsMaintenanceHistory
WHERE UpdateDate > DATEADD(DAY, -1, GETDATE())
ORDER BY UpdateDate DESC;
-- Summary by run
SELECT Run, MIN(UpdateDate) AS RunDate,
COUNT(*) AS StatsUpdated,
SUM(CASE WHEN Outcome = 'Success' THEN 1 ELSE 0 END) AS Succeeded,
SUM(CASE WHEN Outcome = 'Failure' THEN 1 ELSE 0 END) AS Failed
FROM optimize.StatisticsMaintenanceHistory
GROUP BY Run
ORDER BY Run DESC;