Index maintenance without the maintenance job
Except for May, November and December
Jeff Taylor
Principal Data Consultant
Database Consulting, LLC
Super Hero Suit
Index maintenance without the maintenance job
The problem we've all lived with
A true story · the cost of doing it by hand
The job built to protect availability is the thing that took us down.
Agenda
Bloat 101
DENSE ≈99%
BLOATED ≈53%
Same rows. Far more pages — every read touches more of them.
A reframe
Compaction optimizes the metric that matters — and accepts the one that doesn't.
A background process that consolidates partially-filled pages and removes empty ones — continuously, as data changes. You turn it on like this:
ALTER DATABASE [YourDatabase] SET AUTOMATIC_INDEX_COMPACTION = ON;Availability
Under the hood
It rides the Persistent Version Store (PVS) cleaner — the same background process used by Accelerated Database Recovery.
It only touches recently modified pages — never the whole index. That's why overhead is a fraction of a rebuild.
Don't take my word for it
All scripts come straight from Microsoft's blog and Learn docs — nothing cooked up to look good.
Demo 1
-- Turn it on. No restart, no exclusive access.
ALTER DATABASE CURRENT SET AUTOMATIC_INDEX_COMPACTION = ON;
-- Verify across the server
SELECT database_id, name, is_automatic_index_compaction_on
FROM sys.databases;Watch is_automatic_index_compaction_on flip to 1.
Demos 2–4
-- 50,000 tightly-packed rows (pristine, ~99.5% dense)
INSERT INTO dbo.t (s)
SELECT REPLICATE('c',50) FROM GENERATE_SERIES(1,50000);
-- dbo.churn: random INSERT/UPDATE/DELETE/SELECT, 1–100 rows
WHILE @i < @Iterations BEGIN EXEC dbo.churn; SET @i+=1; END;Baseline ≈ 99.51% density · 962 pages · 25 logical reads. Now the workload shreds it.
Demo 5 · run #2
A normal day of writes — the exact state a nightly job exists to fix. Watch what happens if I do nothing.
Demo 5 · run #3 — the payoff
25
Before
1,610
After workload
35
After compaction
| Before | After WL | |
|---|---|---|
| Logical reads | 25 | 1,610 |
| Page density | 99.51% | 52.71% |
≈ 98%
fewer logical reads — automatically
Decision guide
| Aspect | Auto compaction | Reorganize |
|---|---|---|
| Trigger | Continuous, automatic | Manual / scheduled |
| Pages processed | Recently modified only | All pages |
The honest limits · read the label
Also: it slows file growth, but it's not a shrink — it won't hand allocated space back to the OS.
Safety & cost
Demos 6–7
-- #1 Database-wide density & fragmentation rollup
SELECT ... FROM sys.dm_db_index_physical_stats(
DB_ID(), DEFAULT, DEFAULT, DEFAULT, 'SAMPLED') ...
-- #2 Extended Event: rows moved, pages deallocated
CREATE EVENT SESSION automatic_index_compaction ON DATABASE
ADD EVENT sqlserver.auto_index_compaction_stats ...;Fires every 10 min & is cumulative.
Monday morning
1. Enable in non-prod — watch your own metrics for a week
2. One-time rebuild — already bloated? Reset density once; compaction maintains it
3. Add a stats job — if you relied on rebuilds for statistics
4. Retire the old jobs — once you trust it, take your window back
The payoff
The limits are real but narrow: no defrag, no statistics update. Plan for those two and it's a clear win.
If you remember three things
Thank you for attending my session today.
If you have any additional questions, please don't hesitate to reach out.