Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

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.

~5 min readSQL
#mysql#innodb#indexes#utf8mb4
Bold white headline reading KEY TOO LONG with a MySQL index crate overflowing an amber 3072 byte limit gate on the right.

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 formatMax index key lengthDefault since
REDUNDANT767 bytesvery old
COMPACT767 bytesMySQL 5.6
DYNAMIC3072 bytesMySQL 5.7.9, MariaDB 10.2
COMPRESSED3072 bytes5.7+
MyISAM1000 bytesn/a

Bytes per character by charset, which is what turns a character count into a byte count:

CharsetMax bytes per charVARCHAR(255) index sizeMax indexed chars
ascii12553072
latin112553072
utf8mb337651024
utf8mb441020768

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:

ApproachUniqueness on full valueIndex sizeWorks for TEXT
full index on a shortened columnyessmallno
prefix index (col(N))no, only on the prefixboundedyes
generated hash columnyes, with an astronomically small collision chance32 bytesyes
FULLTEXT indexn/a, not a uniqueness constraintseparate structureyes

Neighbouring errors that arrive in the same migration:

ErrorMeaningFix
1071key longer than the row format allowsprefix, shorten, or hash
1170BLOB/TEXT column in a key without a key lengthadd (N)
1406data too long for the columnwiden, or validate input
1118row size too large (the table, not the key)reduce columns or use TEXT
1709index column too long at that prefix lengthlower N

Choosing the prefix length:

ColumnPractical prefixReasoning
email191191 × 4 = 764, fits even a 767-byte limit
URL768maximum under 3072 with utf8mb4
UUID stored as textfull 36144 bytes, no reason to prefix
name100 to 191selectivity usually saturates early
slug2551020 bytes, fits

Practical Edge Cases, Error Fixes & Deep Dive Code Examples

1. The 191 characters rule and where it came from

sql
-- 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:

sql
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 exactly

Long 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 conversion

Check 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)); -- legal

TEXT 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 documentation

The SQL documentation that defines the behaviour this guide explains.

Try matching browser tools

All tools

Small utilities that fit SQL work — they run entirely in this browser tab, so nothing you paste leaves your device.

All SQL guides

More in SQL

All SQL guides

The guides filed next to this one in SQL, in library order.