MySQL 1071: Specified key was too long; max key length is 3072 bytes
1071 appears when an index exceeds 3072 bytes, usually after a utf8mb4 conversion. Prefix indexes fix lookups; generated hash columns keep uniqueness.

Introduction & Quick Summary
A migration or a CREATE TABLE fails:
ERROR 1071 (42000): Specified key was too long; max key length is 3072 bytes
or, on an older server:
ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes
The index you asked for needs more bytes per row than InnoDB allows for a secondary key. The column may look short in characters — VARCHAR(255) is 255 — but InnoDB measures the index in bytes, and utf8mb4 stores up to four bytes per character. 255 × 4 = 1020 bytes, which fits, while VARCHAR(1000) at 4000 bytes does not. The classic trigger is a unique index on an email or URL column that was widened, or a utf8mb4 conversion of a schema designed under latin1.
Quick answer, in order of preference:
-- 1. index a prefix when the column does not need a full-text index
ALTER TABLE articles ADD KEY idx_url (url(768));
-- 2. or shorten the column to what the data actually needs
ALTER TABLE articles MODIFY url VARCHAR(750) NOT NULL;
-- 3. for true uniqueness on long values, index a hash instead
ALTER TABLE articles
ADD COLUMN url_hash BINARY(32) AS (UNHEX(SHA2(url, 256))) STORED,
ADD UNIQUE KEY uq_url_hash (url_hash);A prefix index on a unique key does not enforce uniqueness on the full value: UNIQUE KEY (url(768)) treats two URLs that share their first 768 bytes as duplicates. If the constraint matters, use the hash column.
Core Mechanics, Signatures & Reproducible Setup
InnoDB's limit depends on the row format, which depends on the server version:
| Row format | Max index key length | Default since |
|---|---|---|
REDUNDANT | 767 bytes | very old |
COMPACT | 767 bytes | MySQL 5.6 |
DYNAMIC | 3072 bytes | MySQL 5.7.9, MariaDB 10.2 |
COMPRESSED | 3072 bytes | 5.7+ |
MyISAM | 1000 bytes | n/a |
Bytes per character by charset, which is what turns a character count into a byte count:
| Charset | Max bytes per char | VARCHAR(255) index size | Max indexed chars |
|---|---|---|---|
ascii | 1 | 255 | 3072 |
latin1 | 1 | 255 | 3072 |
utf8mb3 | 3 | 765 | 1024 |
utf8mb4 | 4 | 1020 | 768 |
Reproduce it safely on a scratch schema:
CREATE DATABASE k1071; USE k1071;
CREATE TABLE t (
id INT PRIMARY KEY,
url VARCHAR(1000) NOT NULL,
KEY idx_url (url)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- ERROR 1071 (42000): Specified key was too long; max key length is 3072 bytes
ALTER TABLE t ADD KEY idx_url (url(768)); -- succeeds
SELECT ROW_FORMAT FROM information_schema.TABLES WHERE TABLE_SCHEMA='k1071';1000 × 4 = 4000, which exceeds 3072 by 928 bytes. The prefix form is legal because 768 × 4 = 3072 exactly.
Key Comparison Table
The three fixes and what each actually guarantees:
| Approach | Uniqueness on full value | Index size | Works for TEXT |
|---|---|---|---|
| full index on a shortened column | yes | small | no |
prefix index (col(N)) | no, only on the prefix | bounded | yes |
| generated hash column | yes, with an astronomically small collision chance | 32 bytes | yes |
FULLTEXT index | n/a, not a uniqueness constraint | separate structure | yes |
Neighbouring errors that arrive in the same migration:
| Error | Meaning | Fix |
|---|---|---|
| 1071 | key longer than the row format allows | prefix, shorten, or hash |
| 1170 | BLOB/TEXT column in a key without a key length | add (N) |
| 1406 | data too long for the column | widen, or validate input |
| 1118 | row size too large (the table, not the key) | reduce columns or use TEXT |
| 1709 | index column too long at that prefix length | lower N |
Choosing the prefix length:
| Column | Practical prefix | Reasoning |
|---|---|---|
| 191 | 191 × 4 = 764, fits even a 767-byte limit | |
| URL | 768 | maximum under 3072 with utf8mb4 |
| UUID stored as text | full 36 | 144 bytes, no reason to prefix |
| name | 100 to 191 | selectivity usually saturates early |
| slug | 255 | 1020 bytes, fits |
Practical Edge Cases, Error Fixes & Deep Dive Code Examples
1. The 191 characters rule and where it came from
-- MySQL 5.6, utf8mb4, COMPACT row format: 767 bytes is the ceiling
VARCHAR(191) -- 191 * 4 = 764 bytes, the largest that fits
-- MySQL 5.7.9+ and 8.0 with DYNAMIC: 768 is the new ceiling
VARCHAR(768)Framework defaults from that era — Django's utf8mb4 migration notes, Laravel's Schema::defaultStringLength(191) — encode the 191 limit permanently. On a modern server the limit is unnecessary, and applying it to a new schema costs the ability to index longer values later.
2. A prefix index is not a unique constraint
-- accepts both rows: they differ only after byte 768
INSERT INTO articles (url) VALUES (CONCAT(REPEAT('a', 800), '/one'));
INSERT INTO articles (url) VALUES (CONCAT(REPEAT('a', 800), '/two'));Both inserts succeed against UNIQUE KEY (url(768)), and the duplicate check at the application layer then sees two rows that look identical in the index. When correctness depends on the constraint, add the hash column and keep the prefix index only for lookups.
3. Hash column for long unique values
ALTER TABLE articles
ADD COLUMN url_hash BINARY(32)
GENERATED ALWAYS AS (UNHEX(SHA2(url, 256))) STORED,
ADD UNIQUE KEY uq_url_hash (url_hash),
ADD KEY idx_url (url(768));A stored generated column is maintained by the server, so no application change is needed, and a 32-byte binary key is cheaper than any prefix of a 4-byte-per-character column. Query by hash, then compare the full value:
SELECT id FROM articles WHERE url_hash = UNHEX(SHA2(?, 256)) AND url = ?;The second predicate matters: a hash match alone is a probabilistic answer, and a 256-bit collision is unlikely but the comparison costs nothing.
4. Composite keys share the budget
The limit applies to the whole key, not to each column:
KEY idx_multi (a VARCHAR(500), b VARCHAR(500), c VARCHAR(500)) -- 6000 bytes
-- ERROR 1071
KEY idx_multi (a(256), b(256), c(256)) -- 3072 exactlyLong composite indexes often indicate a missing normalisation step; indexing three 500-character columns together is rarely more selective than indexing the first two.
5. Converting latin1 to utf8mb4
ALTER TABLE posts CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci;
-- ERROR 1071 if any index exceeds 3072 bytes after conversionCheck before converting rather than after a failed migration:
SELECT table_name, index_name, column_name, sub_part
FROM information_schema.STATISTICS
WHERE table_schema = 'app' AND sub_part IS NULL
AND column_name IN ('url','email','title');A column with sub_part set is already a prefix index and converts without growth; the ones with NULL are the risk.
6. TEXT columns need a key length
ALTER TABLE logs ADD KEY idx_msg (message); -- ERROR 1170
ALTER TABLE logs ADD KEY idx_msg (message(255)); -- legalTEXT has no maximum length, so MySQL cannot size an index on it without a prefix. That is why TEXT columns appear in WHERE clauses but rarely in indexes, and why promoting a VARCHAR to TEXT during a migration can break the indexes that used to work.
Common Compilation/Runtime Mistakes & Version Gotchas
Raising innodb_large_prefix and assuming it applies. The variable was deprecated in 5.7.7 and removed in 8.0, where DYNAMIC is the default. A migration script that sets it fails on 8.0 with Unknown system variable, and the 3072 limit still applies.
Prefixing a key to satisfy the parser and keeping the constraint. As shown above, UNIQUE KEY (col(191)) silently enforces uniqueness on 191 characters only. Applications relying on the database for correctness need the hash column.
Assuming 3072 means 3072 characters. The unit is bytes, and the multiplier is the charset's maximum width, not the width of the data currently stored. utf8mb4 is four bytes even for text that is entirely ASCII.
Version differences. MariaDB 10.2 and later default to DYNAMIC and allow 3072 bytes the same way, but MariaDB also supports utf8mb4 indexes in COMPACT with the same 767-byte ceiling, so a schema migrated between the two engines can fail on only one of them. MySQL 8.0.13 added functional indexes, which allow KEY ((SHA2(url, 256))) without a stored column, at the cost of evaluating the expression on every write. utf8mb3 (utf8 in older DDL) uses three bytes and is deprecated in 8.0.28+, so a schema still on utf8 reports different limits than the one you expect.
Checking the resulting schema rather than trusting the migration. SHOW CREATE TABLE prints the effective key length and row format. Diffing two servers' output with the text diff tool is the quickest way to see that production is on COMPACT while staging is on DYNAMIC, which explains a migration that passes in one environment and fails in the other. The SQL formatter is useful for reading a long ALTER TABLE before running it, and building a scratch schema with the schema designer confirms the key sizes before the statement reaches a real table.
Reference material: Troubleshooting row size too large errors with InnoDB and InnoDB limitations.
Official references
primary documentationThe SQL documentation that defines the behaviour this guide explains.
Try matching browser tools
All toolsSmall utilities that fit SQL work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related SQL guides
All SQL guidesSQLite: database is locked (SQLITE_BUSY) under concurrent writes
SQLite allows one writer. Set a busy timeout, switch to WAL, start write transactions as IMMEDIATE, and reduce to a single writer connection.
Postgres: column does not exist (SQLSTATE 42703)
42703 means the identifier you wrote is not the one that exists. Check case folding, alias scope, search_path and migration drift.
PostgreSQL: canceling statement due to statement timeout
The statement was canceled by statement_timeout. Find whether the role, the pooler or the client set it, then scope a longer budget to that job.
pg_dump: server version mismatch, aborting because of server version
pg_dump refuses to read a newer server. Install a matching client, run it from a container or the server host, and match pg_restore too.
More in SQL
All SQL guidesThe guides filed next to this one in SQL, in library order.