wasAsked.

How would you migrate a large SQL table with near-zero downtime in a .NET application?

CRED · Full Stack Developer · 3-4-years · technical

Published

Year not specified · .NET, SQL, DevOps

Community answers

Posted by wasAsked editorial collection

Use expand-and-contract. First add nullable columns, new tables, or indexes without changing existing behavior. Deploy code that reads both formats and writes compatibly, backfill in small resumable batches, verify counts/checksums, switch reads, then enforce constraints and remove old structures in a later release. Avoid one huge transaction and table-rewriting operations during peak traffic. Estimate locks and log growth, set timeouts, throttle batches, monitor blocking/replication, and have explicit pause/rollback criteria. For tenant data, every backfill and verification query must preserve RestaurantId and OutletId. Back up and rehearse on production-sized data; an application rollback cannot undo an incompatible schema change.

Backbencher Developer · · 0 likes

For large tables the danger is long locks, blocked transactions and a long rollback if something fails. The safe approach is incremental and backward compatible. 1. Plan with expand and contract - Never rename or change a column type in place in one step. Add the new structure, migrate gradually, switch over, then remove the old structure in a later release. 2. Expand - Add new columns as nullable (or with a default; adding a NOT NULL column with a constant default is a metadata-only operation in SQL Server 2012+ Enterprise and in modern PostgreSQL). - Create indexes online: CREATE INDEX ... WITH (ONLINE = ON) in SQL Server Enterprise or Azure SQL; CREATE INDEX CONCURRENTLY in PostgreSQL. - Deploy application code that writes both old and new columns (dual-write) and can read either. 3. Backfill in batches WHILE 1 = 1 BEGIN UPDATE TOP (5000) Orders SET NewStatus = LegacyStatus WHERE NewStatus IS NULL; IF @@ROWCOUNT = 0 BREAK; WAITFOR DELAY '00:00:01'; END - Small batches keep locks and log growth short; pause between batches to protect normal traffic. - Run it as a resumable background job, monitor progress, and run it outside peak hours if possible. 4. Switch reads - Release code that reads the new column. Verify with checks comparing old and new data. 5. Contract - After all instances run the new code and you no longer need to roll back, stop writing the old column, then drop it in a later deployment. For whole-table restructuring - Create a new table, copy data in batches, keep it in sync with triggers or change data capture, then switch with a quick rename inside a short transaction, or use a view or synonym. - Tools like gh-ost (MySQL) or pg_repack (PostgreSQL) automate parts of this. Process - Rehearse on a production-sized copy and measure timings. - Take a backup or restore point; prepare the rollback steps. - Run EF Core migrations as reviewed scripts in the pipeline, not automatically at app startup. - Monitor blocking, log usage and replication lag while running.

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics