Safely Alter Large MySQL Tables with Online DDL

Altering large production MySQL tables can cause downtime, lock databases, and degrade application performance if done incorrectly. This article explains how to safely execute schema changes on massive MySQL tables using native Online DDL features, alongside robust external tools like pt-online-schema-change and gh-ost. You will learn how to evaluate algorithm choices, manage database resources, and implement best practices to alter production databases without interrupting user traffic.

Understanding Native MySQL Online DDL

MySQL (specifically the InnoDB storage engine) supports native Online DDL, which allows you to run ALTER TABLE statements while concurrent DML (insert, update, delete) operations continue. To control how the alter is executed, you should explicitly define the ALGORITHM and LOCK clauses.

The ALGORITHM Clause

The LOCK Clause

Safe Native Online DDL Syntax

To ensure your native alter statement does not accidentally block production traffic, explicitly state the safest options:

ALTER TABLE orders ADD COLUMN discount_code VARCHAR(20) DEFAULT NULL,
ALGORITHM=INSTANT, LOCK=NONE;

If INSTANT is not supported for your specific alteration, fallback to INPLACE:

ALTER TABLE orders ADD INDEX idx_user_created (user_id, created_at),
ALGORITHM=INPLACE, LOCK=NONE;

Limitations of Native Online DDL

While native Online DDL is highly effective, it has limitations on massive tables (e.g., hundreds of gigabytes or terabytes):

Utilizing Third-Party Tools for Large Scale Changes

When native Online DDL poses too much risk to replication or performance, industry-standard third-party tools should be used.

pt-online-schema-change (Percona)

This tool creates a ghost copy of the target table, applies the schema changes to the empty ghost table, and creates triggers on the original table to copy concurrent writes. It then copies data from the original table to the ghost table in small, manageable chunks. Once complete, it atomically swaps the tables.

gh-ost (GitHub Online Schema Migrator)

gh-ost is a triggerless online schema migration tool. Instead of triggers, it parses the MySQL binary log (binlog) in real-time to capture concurrent writes and apply them to the ghost table.

Best Practices for Production Execution

To guarantee a safe schema migration on a large production MySQL database, adhere to the following checklist:

  1. Validate on Staging: Run the exact DDL statement or tool execution on a staging database populated with production-scale data to measure execution time, disk space usage, and IOPS.
  2. Verify Disk Space: Ensure you have at least 1.5 to 2 times the table’s current size in free disk space before running an inplace rebuild or using third-party migration tools.
  3. Monitor Replication Lag: If using replicas, monitor lag closely. Configure tools like pt-online-schema-change or gh-ost to automatically pause if replica lag exceeds a safe threshold (e.g., 10 seconds).
  4. Execute During Off-Peak Hours: Even with online tools, heavy schema changes consume disk I/O and CPU. Run these operations during periods of low application traffic.
  5. Configure Timeouts: Set safe session timeout variables to prevent a hung metadata lock from stalling the application if the DDL operation gets blocked at the start or end of its run.