brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: ask MySQL for ALGORITHM=INSTANT so ALTER TABLE never rebuilds silently

Mysql
~/tips/tip-mysql-alter-instant

ALTER TABLE orders ADD COLUMN note VARCHAR(100), ALGORITHM=INSTANT;

Makes a schema change fail fast instead of starting a long table rebuild.

Adding a column can take a blink or a very long time, and the statement looks identical in both cases. Say which one you expect.

Name the algorithm at the end:

ALTER TABLE orders ADD COLUMN note VARCHAR(100), ALGORITHM=INSTANT;

INSTANT only changes metadata. If MySQL can’t do your change that way, you get an error and nothing happens, which is exactly the point. You can then plan the slower method for a quiet moment.

It is already the default for operations that support it in 8.4. Writing it out turns a hope into a requirement, so a migration script can’t quietly fall back to copying a huge table during working hours.

Some tables don’t qualify, among them compressed ones and tables with a FULLTEXT index. There is also a ceiling on how many instant column additions and drops a table can collect before it needs rebuilding. You can watch that count:

SELECT NAME, TOTAL_ROW_VERSIONS
FROM INFORMATION_SCHEMA.INNODB_TABLES
WHERE NAME = 'shop/orders';

A rebuild with OPTIMIZE TABLE resets it to zero. The limit is 64 in 8.4.

Instant doesn’t mean lock free. The statement still needs a brief metadata lock, so one long open transaction on the table can leave it waiting, with new queries piling up behind it. Check for old transactions first, and I’d still try it on a copy before production.

Further reading: Online DDL operations

More Mysql tips FOR UPDATE SKIP LOCKED; mysql_config_editor set --login-path=shop \ SET PERSIST max_connections = 300; ALTER USER 'appuser'@'192.0.2.10' SELECT * FROM sys.schema_unused_indexes All Mysql tips (8) All topics

Link