Online index rebuild or transactional offline DDL: which keeps schema-change rollback safer?
0 reputation · 28 Jun 2024, 15:32 UTC
0 reputation · 28 Jun 2024, 15:32 UTC
I'm planning a schema migration on SQL Server (Enterprise Edition, recent version) that alters a large, heavily queried table. Two documented approaches are on the table: an online index rebuild with ONLINE = ON, which permits concurrent DML, or an offline change wrapped in BEGIN TRANSACTION so the DDL can roll back if anything fails mid-deployment.
The tension I can't resolve: the offline path acquires a Schema Modification (Sch-M) lock that blocks all table access, and the documented behavior is that rolling back a large change can take roughly as long as the original operation — so a failed migration could extend the outage rather than shorten it. The online path avoids most blocking but generates significant transaction log growth from row versioning, and I'm unsure how that interacts with log shipping and disk headroom during a long rebuild.
My concrete constraint is a short maintenance window on a multi-terabyte table where a failed deployment must not leave the application blocked for hours.
Given that constraint, which approach offers the more predictable worst-case recovery time? Does wrapping an online operation in an explicit transaction meaningfully change rollback behavior? And what should I monitor (e.g., sys.dm_tran_locks, log usage) to decide before committing in production?
31775 reputation · 29 Jun 2024, 03:11 UTC
An online rebuild records undo information in the transaction log and version store while allowing concurrent DML. The amount of log generated bounds how long a rollback can take, so even if the operation fails the recovery time is proportional to the log size and usually shorter than re‑doing the whole change.
sys.dm_db_log_space_usage and tempdb version‑store usage via sys.dm_tran_version_store.sys.dm_tran_locks for the absence of Sch‑M locks and sys.dm_exec_requests for percent_complete and log growth rate.If the schema change forces the rebuild offline, the offline path’s rollback time becomes unpredictable and may exceed your maintenance window; in that case consider a staged change or partitioning strategy instead.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.