🗄️ Fix an XML Index Rebuild Timing Out Nightly
A primary XML index has to be rebuilt in full whenever its underlying table undergoes significant change, since XML indexes don’t support the same incremental maintenance path as a standard rowstore index, and a nightly maintenance job that rebuilds it unconditionally — rather than checking whether it actually needs rebuilding — can simply outgrow its maintenance window as the table’s XML column grows, timing out partway through and leaving the index in a degraded, partially-rebuilt state until the next run.
🔎 The Problem
-- Nightly job, unconditional every run: ALTER INDEX PXML_Documents_Content ON dbo.Documents REBUILD; -- Table has grown from 200k to 1.4M rows over 6 months -- Job's maintenance window (60 min) no longer covers full rebuild time -- Job log: "Timeout expired" partway through, index left degraded
✅ Fix: Rebuild Only When It’s Actually Needed, With Room to Finish
- Check fragmentation via
sys.dm_db_xml_index_physical_statsbefore rebuilding, and skip the rebuild entirely on nights where fragmentation is already low, instead of paying the full cost unconditionally every single run. - If the table has genuinely outgrown the maintenance window, split the XML index rebuild into its own longer window separate from the rest of the nightly maintenance plan, rather than letting it compete for time with unrelated jobs.
- Monitor rebuild duration over time as a trend, not just success/failure of the last run — a job that’s taking 10% longer each month gives you weeks of warning before it actually times out.
An index that worked fine at 200,000 rows doesn’t get a vote on how long it’s allowed to take at 1.4 million — the maintenance window does.



