Introduction
Running an ALTER TABLE query on a MySQL table with millions of rows can lock writes for minutes or hours.
On high-volume e-commerce platforms and SaaS backends, table locks immediately cascade into connection timeouts, gateway 504 errors, and lost customer orders.
Executing database migrations without taking the application offline requires structured online schema migration techniques.
Here is how senior infrastructure engineers alter schemas safely on production MySQL and MariaDB clusters.

Using MySQL Native Online DDL Features
Modern MySQL (version 8.0+) includes native Online DDL capabilities with the ALGORITHM=INPLACE, LOCK=NONE parameters.
Adding indexes or secondary keys can often be performed in place without blocking concurrent INSERT and UPDATE statements.
However, modifying data types, dropping columns, or rebuilding primary keys still demands exclusive metadata locks that can stall production queues.
Always verify with EXPLAIN and dry runs on staging copies before initiating alterations in live production.
Implementing Shadow Table Migrations (gh-ost and pt-online-schema-change)
For mission-critical tables where even fleeting metadata locks are intolerable, use tools like GitHub's gh-ost or Percona's pt-online-schema-change.
These utilities create an empty shadow copy of the table, apply the desired schema changes, and stream changes asynchronously.
Gh-ost inspects binary logs directly rather than relying on heavy SQL triggers, eliminating lock contention.
Once the shadow table is fully synchronized, a lightning-fast atomic RENAME TABLE command swaps the old and new tables in milliseconds.
Backwards-Compatible Code Deployments (Expand and Contract)
Database migrations must always occur in lockstep with backwards-compatible application code.
Follow the expand-and-contract pattern: first add new columns without dropping legacy fields.
Deploy application code that writes to both columns and reads from the new one.
Only after verifying data consistency across all services should a subsequent cleanup migration drop the obsolete columns.
Frequently Asked Questions
<strong>Q: Why does a query freeze immediately on ALTER TABLE even with LOCK=NONE?</strong><br>MySQL must acquire a brief Metadata Lock (MDL) at the start and finish of an online DDL operation. If an open transaction or slow SELECT statement is holding an existing lock on that table, the ALTER statement queues up and blocks all subsequent queries behind it.
<strong>Q: How do we monitor replication lag during online schema changes?</strong><br>Tools like gh-ost include built-in replica throttling. Configure --max-lag-millis to pause shadow migration whenever replica lag exceeds acceptable thresholds, protecting read-replica clusters.