Status: Reviewed (deep-dive v1)
Stack: PowerShell (optional host pressure) → SQL jobs/procs → Blazor Server UI
Depends on: SQL-01 Inventory; safer after SQL-02 Backup coverage is trusted
Goal: Keep indexes and stats healthy without wrecking log/IO during business hours.
Fragmented indexes and stale stats cause real pain — but blind rebuilds cause more. Measure, threshold, window, then act. Never maintain what you haven’t inventoried and can’t restore.
| Metric | Reorganize | Rebuild | Skip / watch |
|---|---|---|---|
Fragmentation (avg_fragmentation_in_percent) |
~10–30% | ≥ ~30% | < ~10% |
| Page count | Only if ≥ ~1000 pages (ignore tiny indexes) | Same | Small indexes |
| Stats | UPDATE if modification counter high or sample stale |
After large data loads | Tiny tables |
These are common starting points, not dogma. Partitioned tables, LOBs, columnstore, and AG secondaries need special cases (see below).
Prefer:
REORGANIZE for mid fragmentation (online, cheaper log)REBUILD with ONLINE=ON when edition supports and window allowsUPDATE STATISTICS with smart sample rather than blanket fullscan nightlyRun against inventoried instances in a maintenance/eval job (read-heavy).
sys.dm_db_index_physical_stats (LIMITED mode for estate scans)sys.stats + sys.dm_db_stats_properties (rows, modification_counter, last_updated)Avoid scanning every DB with DETAILED mode on a large estate during peak.
| Table | Purpose |
|---|---|
dbo.IndexMaintPolicy |
Per-instance or per-db thresholds + window |
dbo.IndexHealthSnapshot |
Latest frag/page stats sample |
dbo.StatsHealthSnapshot |
Latest stats modification sample |
dbo.IndexMaintQueue |
Planned actions (Reorg/Rebuild/UpdateStats) + status |
dbo.IndexMaintRun |
Job run audit (start/end, counts, errors) |
Procs / jobs:
usp_IndexHealth_ApplySnapshot — store sample; enqueue actions over thresholdsusp_IndexMaint_GetTopOffenders — Blazor gridusp_IndexMaint_GetLastRun — job status cardExecution engine can be T-SQL job steps or PS calling T-SQL; keep commands logged.
IndexMaintRun error; check log disk, blocking, timeout| Situation | Guidance |
|---|---|
| Always On secondary | Readable secondary: reorg sometimes OK; rebuilds usually on primary; respect backup preference / sync lag |
| Enterprise vs Standard | ONLINE rebuild not on all editions; plan downtime or reorg-only |
| Columnstore | Different DMVs / rebuild semantics — track separately later |
| LOB / XML / spatial | Rebuild cost high; test in nonprod |
| Fill factor | Don’t change casually; document if used for hotspot tables |
| Ola Hallengren / Minion / custom | If already present, document which; don’t run two competing jobs |
SQL Agent jobs & schedules — monitor the jobs that run this maintenance (and backups).
SQL Dude — SQL-03 Index & Statistics Maintenance v1