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:

ConditionThresholdRationale
No modifications + old> 30 days since updateStats may be outdated even without changes
Has modifications + aging> 7 days since updateAllow batching rather than constant updates
Large tables with mods> 5% modification rateHigh-row tables need tighter thresholds
Small tables with mods> 20% modification rateSmall tables tolerate more churn

Exclusions: CDC schema, empty tables, system tables.

Parameters

ParameterDefaultDescription
@DatabasesUSER_DATABASESSelect databases using keywords, wildcards, and exclusions. See syntax
@Run11 = execute updates and log to history table (default). 0 = dry run — display stale statistics without updating, useful for previewing what would change
@StatisticsSampleNULLNULL = SQL Server default. 100 = FULLSCAN. 1–99.99 = sample percentage
@TableNameNULLNULL = all tables. Supports CSV with optional schema prefix (dbo.MyTable)
@DetailOutput00 = minimal output (database progress only). 1 = detailed results per statistic
@PurgeAfterDays90Purge history records older than this many days

Logging

Every update is logged to optimize.StatisticsMaintenanceHistory:

ColumnDescription
Database, Schema, Table, StatisticNameIdentifies the specific statistic updated
LastUpdatedWhen the stat was last updated before this run
DaysSinceUpdateHow stale the stat was
ModificationCountNumber of row modifications since last update
RowCount, ModificationPctTable size and modification rate
OutcomeSuccess or Failure
RunGroups 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;