Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

Why can ALTER TABLE be dangerous on a large MySQL table in production, and what do ALGORITHM=INSTANT, INPLACE and COPY mean?

ALTER TABLE can be dangerous because some changes rebuild the whole table, which can take hours on a large one, and even online changes need a brief exclusive metadata lock at the start and end that waits behind long transactions and, while it waits, blocks every new query on the table. ALGORITHM=INSTANT changes only metadata; INPLACE works inside InnoDB and allows concurrent writes for many operations but still costs I/O and replica lag; COPY builds a new table and copies the rows while blocking writes. Specifying ALGORITHM and LOCK explicitly makes MySQL fail fast instead of silently using a slower, blocking method.

Why this question is asked

Running a schema migration on a small table takes milliseconds, so many teams learn about ALTER TABLE's dangers only when a migration on a large production table freezes the application. Interviewers ask this question because the three algorithms and the metadata lock explain almost every migration outage on MySQL. In this answer, we will recap what a metadata lock is, see how an ALTER can block all queries, go through INSTANT, INPLACE and COPY with examples, and then see how to write migrations that fail safely.

1A Quick Recap of Metadata Locks

Every query that uses a table takes a metadata lock, MDL, on it: a shared lock that says I am using this table's definition, do not change it under me. A DDL statement needs an exclusive metadata lock, because it changes the definition. Shared locks are compatible with each other, so normal queries never notice them. An exclusive lock must wait until every current holder of a shared lock has finished, and that includes open transactions that touched the table earlier, because a transaction keeps its metadata locks until it commits.

2How an ALTER Freezes the Whole Table

This is the part that surprises people. While the ALTER is waiting for its exclusive lock, MySQL does not let new shared lock requests jump ahead of it, otherwise a busy table would starve DDL forever. So every new query on the table, even a plain SELECT, queues behind the waiting ALTER.

SQL
-- Session 1: a report that started a transaction and is still open
START TRANSACTION;
SELECT COUNT(*) FROM orders; -- holds a shared MDL until COMMIT
-- Session 2: the migration
ALTER TABLE orders ADD COLUMN note VARCHAR(200), ALGORITHM=INSTANT; -- waits
-- Sessions 3 to 300: the application
SELECT * FROM orders WHERE id = 42; -- waits behind the ALTER
SHOW PROCESSLIST;
-- ... | Query | 41 | Waiting for table metadata lock | ALTER TABLE orders ...
-- ... | Query | 39 | Waiting for table metadata lock | SELECT * FROM orders ... (x 300)
Step throughA metadata lock queue
Open transactionMySQLALTERApp queries
shared MDL on orders (not committed)
request exclusive MDL
wait: a shared holder exists
SELECT: request shared MDL
wait: an exclusive request is queued
COMMIT releases the shared MDL
exclusive MDL granted, ALTER runs
queries proceed

8 messages in order. Play them, or step through them one at a time.

Notice that the ALTER is INSTANT and would take milliseconds. It is the wait, not the work, that causes the outage. The connection pool fills with waiting queries, the application times out, and everything recovers the moment session 1 commits. lock_wait_timeout defaults to one year, so without a lower value the ALTER will wait practically forever.

3ALGORITHM=INSTANT: Change Only the Metadata

INSTANT, available since MySQL 8.0.12, changes the table definition in the data dictionary without touching any rows. Adding a column was the first case; since 8.0.29 a column can be added at any position and dropped instantly too. Renaming a column, setting or dropping a column default, and adding values at the end of an ENUM are also instant. The statement still needs the brief exclusive metadata lock, so the wait problem above applies, but once it has the lock it finishes in milliseconds whatever the table size. One limit to know: a table allows a limited number of instant row format changes (64 since 8.0.29), after which a rebuild is needed; check your version.

SQL
ALTER TABLE orders ADD COLUMN note VARCHAR(200), ALGORITHM=INSTANT; -- 0.02 sec on 500M rows
ALTER TABLE orders RENAME COLUMN note TO memo, ALGORITHM=INSTANT; -- 0.01 sec

4ALGORITHM=INPLACE: Work Inside InnoDB

INPLACE means InnoDB does the work itself without creating a new table through the server layer. Adding a secondary index is the classic case: InnoDB reads the table, builds the index, and records concurrent changes in a log that it applies at the end, so with LOCK=NONE inserts and updates continue throughout. Some INPLACE operations still rebuild the table internally, such as adding a primary key or changing ROW_FORMAT.

The costs are real even when writes are not blocked. Building an index on a 500 million row table reads the whole table and writes the new index, which competes with production I/O. And the replica applies the same ALTER only after it has finished on the primary, in a single thread, so a three-hour index build gives the replica about three hours of extra lag.

SQL
ALTER TABLE orders ADD INDEX ix_status (status), ALGORITHM=INPLACE, LOCK=NONE;
-- Query OK, 0 rows affected (2 hours 51 min); concurrent DML allowed

5ALGORITHM=COPY and Why We Name the Algorithm

COPY is the oldest method: MySQL creates a new table with the new definition, copies every row into it, and swaps the tables. Concurrent writes are blocked for the whole copy. Some changes still need it, most notably changing a column's data type, such as INT to BIGINT, or converting the character set.

If we write an ALTER without naming the algorithm, MySQL picks the fastest one the change supports, which may be COPY without telling us. Naming ALGORITHM and LOCK turns that into an error instead, which is exactly what we want in a migration.

SQL
ALTER TABLE orders MODIFY qty BIGINT NOT NULL, ALGORITHM=INPLACE, LOCK=NONE;
-- ERROR 1846 (0A000): ALGORITHM=INPLACE is not supported. Reason: Cannot change
-- column type INPLACE. Try ALGORITHM=COPY.
SET SESSION lock_wait_timeout = 5; -- give up after 5 s instead of queueing all traffic
The three algorithms
AlgorithmWhat it doesWrites during itExamples
INSTANTmetadata onlyyes (brief MDL)add or drop column, rename, default
INPLACEInnoDB builds or rebuildsusually, with LOCK=NONEadd secondary index
COPYnew table, copy all rowsblockedchange column type, charset

The error tells us this change needs a different plan, usually an online schema change tool such as gh-ost or pt-online-schema-change, which copy the table in small chunks while traffic continues.

6Follow-up questions

Why does a metadata lock wait block reads too?

Because the waiting ALTER has requested an exclusive metadata lock, and MySQL queues new shared requests behind a pending exclusive one so that DDL is not starved on a busy table. A plain SELECT needs a shared metadata lock, so it waits behind the ALTER, which itself waits for the open transaction. That is why a forgotten open transaction plus an instant ALTER can block every query on the table.

How do we find the transaction blocking the ALTER?

performance_schema.metadata_locks shows granted and pending metadata locks with their owner threads, and sys.schema_table_lock_waits joins them into blocker and waiter pairs. information_schema.innodb_trx ordered by trx_started also shows the oldest open transactions.

Does an INSTANT ADD COLUMN cause replica lag?

Barely. It is just as instant on the replica. It still needs the metadata lock there, so a long transaction on the replica can make the replica's apply wait.

7Conclusion

In this answer, we saw that ALTER TABLE is dangerous on large tables for two reasons: some changes rebuild or copy the whole table, and every DDL needs an exclusive metadata lock that waits behind open transactions while blocking all new queries on the table. INSTANT changes metadata only, INPLACE works inside InnoDB and usually allows writes but costs I/O and replica lag, and COPY rebuilds the table while blocking writes. We name ALGORITHM and LOCK so MySQL fails instead of falling back, and set a short lock_wait_timeout.

What to say in the interviewAbout 90 seconds aloud

Open with

There are two dangers. The obvious one is that some ALTERs rebuild or copy the whole table, which on a large table can take hours and may block writes.

Then, if they want more

The less obvious one is the metadata lock. Every DDL needs an exclusive metadata lock at the start and the end, and it has to wait for every open transaction that touched the table.

While it waits, new queries, even plain SELECTs, queue behind it, so one forgotten transaction plus a harmless ALTER can freeze all traffic on the table. lock_wait_timeout defaults to a year, so I set it to a few seconds for migrations.

The algorithms: INSTANT only changes metadata in the data dictionary, so adding a column, at any position since 8.0.29, dropping a column, renaming one or changing a default takes milliseconds regardless of size.

INPLACE does the work inside InnoDB, for example adding a secondary index, and with LOCK=NONE writes continue, but it still reads the whole table, competes for I/O, and the replica runs it afterwards in one thread, so it adds lag equal to its duration.

COPY creates a new table and copies all rows while blocking writes; changing INT to BIGINT needs it. I always specify ALGORITHM and LOCK explicitly, because then MySQL returns an error instead of quietly falling back to a blocking method, and for COPY-only changes on big tables I use gh-ost or pt-online-schema-change.

About this answer

Last updated
12 November 2026
Versions it names
MySQL 8.0
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
MySQL reference manual
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Why can ALTER TABLE be dangerous on a large MySQL table in production, and what do...": https://scrivio-interview-library.netlify.app/databases/mysql/47f3562bb7/