Running ALTER TABLE on a large InnoDB table is safe when you know which algorithm MySQL will use, whether the table will be rebuilt, how much disk space the operation needs and what could block it. Most outages come from skipping those checks, not from the statement itself. This guide applies to MySQL 8.0 with InnoDB; MariaDB and older MySQL versions behave differently, so confirm against your own version.
In short: prefer ALGORITHM=INSTANT where it is supported, state ALGORITHM and LOCK explicitly so MySQL refuses an unsafe operation instead of silently choosing one, rehearse on a copy of the table, check free space in the data directory and in tmpdir, watch for long transactions that block the metadata lock, and use an online schema change tool when a table rebuild cannot be avoided on a busy production table.
How does MySQL change a large table?
MySQL has three algorithms for ALTER TABLE, described in the MySQL online DDL documentation:
- INSTANT changes only metadata in the data dictionary. The table data is untouched, so the operation finishes almost immediately regardless of table size, and concurrent DML is allowed.
- INPLACE changes the table without copying it to a new table in the usual sense. Some in-place operations still rebuild the table, and these take time proportional to its size, although reads and writes can continue.
- COPY creates a new table, copies every row and swaps the tables. Concurrent writes are blocked while it runs.
Which one applies depends on the operation and the MySQL version. Adding a column uses INSTANT by default from MySQL 8.0.12, and dropping a column from 8.0.29. Before 8.0.29, an instant column could only be added as the last column. There is a limit of 64 row versions per table, and rebuilding the table resets it.
| Operation | Instant | In place | Rebuilds table | Concurrent DML |
|---|---|---|---|---|
| Add column | Yes | Yes | No (with INSTANT) | Yes |
| Drop column | Yes (8.0.29+) | Yes | Yes (without INSTANT) | Yes |
| Add or drop secondary index | No | Yes | No | Yes |
| Extend VARCHAR (same length bytes) | No | Yes | No | Yes |
| Change column data type | No | No | Yes (COPY) | No |
| Reorder columns | No | Yes | Yes | Yes |
| OPTIMIZE TABLE or ALTER … FORCE | No | Yes | Yes | Yes |
Exceptions matter. Instant changes are not available for ROW_FORMAT=COMPRESSED tables or tables with a FULLTEXT index, and adding an auto-increment column does not permit concurrent DML. Extending a VARCHAR from 255 to 256 bytes crosses from one to two length bytes and needs COPY.
How do you state the algorithm and lock explicitly?
If you add ALGORITHM and LOCK clauses, MySQL fails the statement when it cannot honour them, which is what you want on a production table:
ALTER TABLE orders
ADD COLUMN note VARCHAR(255) NULL,
ALGORITHM=INSTANT;
ALTER TABLE orders
ADD INDEX idx_created (created_at),
ALGORITHM=INPLACE, LOCK=NONE;
LOCK=NONE permits concurrent queries and DML, LOCK=SHARED permits queries but blocks writes, and LOCK=EXCLUSIVE blocks both. Omitting LOCK uses the maximum concurrency the operation allows.
How do you rehearse a change on a large table?
The MySQL documentation suggests a simple check. Create an empty copy of the table structure, add a small amount of data, run the same ALTER TABLE and read the rows-affected count. Zero means the table was not copied. A non-zero number means data was copied and you should plan for it.
For timing, restore a recent backup to a staging server with the same MySQL version and similar hardware, and run the real statement there. Duration on a small clone says little about duration on the full table.
How much disk space does ALTER TABLE need?
Instant operations need no extra space. Operations that rebuild the table or create indexes use space in several places:
- Sort files go to the MySQL temporary directory (tmpdir), or to innodb_tmpdir if you set it, and may need space equal to the table data plus index data.
- The intermediate table file is created in the same directory as the original table and may need space equal to the original table. Its name starts with #sql-ib, and innodb_tmpdir does not apply to it.
- The online log records concurrent DML while the operation runs and grows up to innodb_online_alter_log_max_size, which defaults to 128 MB.
Check free space on both file systems before you start. Do not rely on a single multiplier such as table size times 2.5, because the real need depends on the operation, indexes and temporary directory layout.
SELECT @@tmpdir, @@innodb_tmpdir, @@datadir;
SELECT @@innodb_online_alter_log_max_size;
If the online log reaches its limit, the operation fails with DB_ONLINE_LOG_TOO_BIG and uncommitted concurrent DML is rolled back. Raising the limit allows more concurrent writes, but it also lengthens the final phase in which the table is locked to apply the log. On a very busy table the better answer is to run the change in a quieter window or use an external tool.
What can block an ALTER TABLE?
An online ALTER TABLE does not lock the table for its whole duration, but it takes a metadata lock at the start and needs an exclusive metadata lock briefly at the end to commit the new definition. A transaction that is open on the table, even one that has been idle for hours, holds a metadata lock and makes the ALTER wait. While the ALTER waits, it blocks every new query on that table, so a small problem can turn into an outage.
- Long-running or forgotten transactions, including idle sessions with an open transaction.
- LOCK TABLES and FLUSH TABLES WITH READ LOCK, often issued by backup jobs.
- Long reports and exports reading the table.
- Another DDL statement on the same table.
Before you run the change, look for open transactions and lock waits. After you start it, SHOW FULL PROCESSLIST shows the state “Waiting for table metadata lock”, and the Performance Schema metadata_locks table shows which session holds the lock. Set lock_wait_timeout to a low value for the session so a blocked ALTER gives up rather than queueing traffic behind it.
SET SESSION lock_wait_timeout = 10;
SELECT * FROM performance_schema.metadata_locks
WHERE OBJECT_NAME = 'orders';
What about replicas?
A DDL statement is replicated like any other, so a rebuild that takes an hour on the primary can also keep a replica busy for that long, and replication lag grows while it runs. Watch lag during the change. Where lag matters, apply the change in a way you control, for example on each replica in turn, or use an external tool that copies data in small chunks.
When should you use gh-ost or pt-online-schema-change?
When a change needs a rebuild and the table is too busy for a lock or for the online log limit, external tools copy rows to a shadow table in the background and swap it in at the end. gh-ost reads the binary log instead of using triggers, and pt-online-schema-change uses triggers. Both throttle themselves under load and both have limitations with foreign keys, triggers and replication topologies, so read their documentation and rehearse on staging first.
A safe order of work
- Take a verified backup and confirm you can restore it.
- Identify the algorithm for your MySQL version and operation, and state ALGORITHM and LOCK in the statement.
- Rehearse on a restored copy and note duration, space use and replication lag.
- Check free space in the data directory and in tmpdir.
- Check for open transactions, backup jobs and other DDL on the table.
- Run in a low-traffic window, with a short lock_wait_timeout, and monitor.
- Verify the new structure and application behaviour, then document what you changed.
What does this mean for WordPress and WooCommerce?
WordPress rarely needs hand-written ALTER TABLE statements, but large wp_postmeta, wp_options and order tables are where they appear, for example when adding an index to speed up a slow query. Plugin updates may also change table structure during an upgrade, and on a large shop that can take longer than expected. See which database WooCommerce uses for how the tables are laid out.
For slow queries and heavy tables, see our performance work, and an audit can show which queries and tables hold your site back. Ongoing growth and care covers backups and safe maintenance windows, and we build and maintain WooCommerce stores and custom WordPress development projects.
FAQ
Does ALTER TABLE lock the table in MySQL?
It depends on the algorithm and operation. INSTANT and many INPLACE operations permit concurrent reads and writes, but they still take brief metadata locks, and a waiting ALTER can block other queries. Changing a column data type uses COPY and blocks writes.
How long does ALTER TABLE take on a large table?
INSTANT operations finish almost immediately. Rebuilds scale with table size and hardware, so rehearse on a restored copy for a real estimate.
How much free disk space do I need?
Instant operations need none. Rebuilds can need space for sort files in tmpdir and an intermediate table in the data directory, each up to the size of the data and indexes, so check both.
What is innodb_online_alter_log_max_size?
It limits the log that records concurrent DML during an online operation. Its default is 128 MB. If the log exceeds it, the operation fails and uncommitted concurrent DML is rolled back.
Can I stop an ALTER TABLE halfway?
Killing an online in-place operation rolls it back, which can take time, and the table stays usable. Test the behaviour on staging and do not assume an instant rollback.
When should I use gh-ost or pt-online-schema-change?
When the change rebuilds a table that is too busy for the built-in online operation, for example because of heavy write traffic or replication lag.



