Sphere
Log in Join

MySQL 8 'Specified key was too long; max key length is 3072 bytes' with utf8mb4

The migration SQL had been running for about 40 minutes when it hit this, halfway through recreating an index on a table with 2.3 million rows:

ERROR 1071 (42000) at line 218: Specified key was too long; max key length is 3072 bytes

The column was VARCHAR(1024) with utf8mb4, used in a composite index. On the old server the same DDL had worked. The new one was MySQL 8.0.36, the old one 5.7 with innodb_large_prefix set. That difference turned out to be the whole story.

## How the 3072 number is reached

utf8mb4 uses up to four bytes per character. VARCHAR(1024) therefore reserves up to 4096 bytes for the index entry, over InnoDB's 3072-byte limit in modern MySQL. The limit comes from the page size: with the default 16 KB page, InnoDB allows 3072 bytes for one index key. On an 8 KB page it's 1536, on 4 KB it's 768.

The 5.7 server had innodb_large_prefix=ON, which allowed 3072 bytes for DYNAMIC and COMPRESSED row formats. In 8.0 that option was removed and the larger limit became the default, but the row format still matters — if the table is COMPACT or REDUNDANT, you're capped at 767 bytes.

## Check the row format first

This is the thing I should have looked at before anything else:

sql
SELECT TABLE_NAME, ROW_FORMAT
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = 'appdb'
AND ROW_FORMAT NOT IN ('DYNAMIC', 'COMPRESSED');

It turned out our migration was creating tables with an explicit ROW_FORMAT=COMPACT, copied from a 2016-era init script. Under COMPACT the effective limit is 767 bytes, so a VARCHAR(256) in utf8mb4 takes 1024 bytes and fails. That's why the error said 3072 — MySQL reports the global cap, not the per-row-format cap. Confusing.

Fix:

sql
ALTER TABLE events ROW_FORMAT=DYNAMIC;

## The three actual fixes

Depending on what you need the index for:

sql
-- 1. prefix index: index the first 191 chars (191 * 4 = 764 bytes)
CREATE INDEX idx_slug ON events (slug(191));

-- 2. shorten the column if you don't really need 1024
ALTER TABLE events MODIFY slug VARCHAR(255) CHARACTER SET utf8mb4;

-- 3. switch the column if you only store plain ASCII slugs
ALTER TABLE events MODIFY slug VARCHAR(1024) CHARACTER SET utf8mb3;

We went with the prefix index for slug, because slugs in our data are short in practice and the full length was defensive. Option 3 is fine for slugs but I wouldn't use it for user-generated text — you lose emoji and a bunch of CJK characters.

## One caveat with prefix indexes

A prefix index can't be used for a full ORDER BY on that column, and SELECT DISTINCT may behave differently than you expect because two long values with the same first 191 characters count as equal in the index. We checked for collisions before committing:

sql
SELECT COUNT(*) FROM (
SELECT LEFT(slug, 191) p, COUNT(*) c
FROM events GROUP BY p HAVING c > 1
) x;

Zero rows, so we were fine. On a table where that query returns something, you need a hash column instead.

Anyway, ERROR 1071 is almost always a bytes-per-character problem, not a "your string is too long" problem. Check ROW_FORMAT before you start shortening columns.