MySQL & MariaDB error 1071: Specified key was too long
Specified key was too long; max key length is 3072 bytes (5.7.44) An index would be longer than the storage engine allows (3072 bytes for InnoDB with DYNAMIC rows, 767 with the old COMPACT format). utf8mb4 needs 4 bytes per character, so VARCHAR(1000) can't be fully indexed.
| MySQL | MariaDB | |
|---|---|---|
| Symbol | ER_TOO_LONG_KEY | ER_TOO_LONG_KEY |
| SQLSTATE | 42000 | 42000 |
| Type | Server error | Server error |
| Versions | every release since 5.0 active | every release since 5.5 active |
What it means
Specified key was too long; max key length is 3072 bytes. The limit is in bytes, and InnoDB reserves the maximum bytes per character: 4 for utf8mb4, 3 for utf8mb3. So an index on VARCHAR(1000) CHARACTER SET utf8mb4 needs 4,000 bytes.
| Row format / version | Max index prefix |
|---|---|
| DYNAMIC or COMPRESSED (default since MySQL 5.7.9 / MariaDB 10.2) | 3072 bytes, so VARCHAR(768) in utf8mb4 |
| COMPACT or REDUNDANT, or MySQL 5.6 defaults | 767 bytes, so VARCHAR(191) in utf8mb4 |
Why it happens
- Converting a table from utf8/latin1 to utf8mb4: existing indexes grow by a third or more.
- Indexing long URL, path or email columns.
- MySQL 5.6 or
ROW_FORMAT=COMPACTtables: that is where the famousVARCHAR(191)advice comes from. MariaDB reports the COMPACT case as 1709. - Behaviour differs by version (from our tests): MySQL 5.7+ rejects the index with 1071; MySQL 5.6 and MariaDB instead silently create a shorter prefix index for a non-unique key (5.6 with a warning, MariaDB with only a note). For a UNIQUE key MariaDB 10.4+ builds a hidden hash index instead of failing.
How to fix it
- Index a prefix:
KEY (url(191)). A prefix can’t enforce full-value uniqueness, though. - Shorten the column to what you really need, e.g.
VARCHAR(255)for email. - Use
ROW_FORMAT=DYNAMIC(the default on current versions) to get the 3072-byte limit. - For uniqueness on long values, index a hash: a generated column
url_hash BINARY(32) AS (UNHEX(SHA2(url, 256))) STOREDwith a UNIQUE key on it.
Reproduce it yourself
These steps are the exact test our harness runs. Use a scratch server, never production.
Connect with
mysql -u root -pand create a scratch database:CREATE DATABASE demo; USE demo;Run:
CREATE TABLE t (url VARCHAR(1000) CHARACTER SET utf8mb4, KEY (url)) ENGINE=InnoDB;
You get: Specified key was too long; max key length is 3072 bytes(9.7.4)
Verified fixes
Index a prefix of the column, or shorten it
CREATE TABLE t (url VARCHAR(1000) CHARACTER SET utf8mb4, KEY (url(191))) ENGINE=InnoDB; SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 't';
Tested on
| Step | MySQL | MariaDB | |||||||
|---|---|---|---|---|---|---|---|---|---|
| 5.6 | 5.7 | 8.0 | 8.4 | 9 | 10.6 | 10.11 | 11.4 | 11.8 | |
| Index a long utf8mb4 VARCHAR (4 bytes per character); MySQL 5.6 and MariaDB silently index a prefix instead repro | ≠ | ✓ 1071 | ✓ 1071 | ✓ 1071 | ✓ 1071 | ≠ | ≠ | ≠ | ≠ |
| Same column as a UNIQUE key (MariaDB 10.4+ uses a hidden hash index) version check | ✓ 1071 | ✓ 1071 | ✓ 1071 | ✓ 1071 | ✓ 1071 | ≠ | ≠ | ≠ | ≠ |
| VARCHAR(255) utf8mb4 index (1020 bytes) with ROW_FORMAT=COMPACT: 767-byte limit (MariaDB reports 1709) version check | ≠ | ✓ 1071 | ✓ 1071 | ✓ 1071 | ✓ 1071 | ✓ 1709 | ✓ 1709 | ✓ 1709 | ✓ 1709 |
| Index a prefix of the column, or shorten it fix | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
What the warnings and failures say (3)
- Index a long utf8mb4 VARCHAR (4 bytes per character); MySQL 5.6 and MariaDB silently index a prefix instead (MySQL 5.6, MariaDB 10.6, MariaDB 10.11, MariaDB 11.4, MariaDB 11.8): expected error 1071 did not occur
- Same column as a UNIQUE key (MariaDB 10.4+ uses a hidden hash index) (MariaDB 10.6, MariaDB 10.11, MariaDB 11.4, MariaDB 11.8): expected error 1071 did not occur
- VARCHAR(255) utf8mb4 index (1020 bytes) with ROW_FORMAT=COMPACT: 767-byte limit (MariaDB reports 1709) (MySQL 5.6): expected error [1071, 1709] did not occur