Lab-verifiedReproduced and fixed on MySQL 8.4.13 · 2026-12-01 · container image mysql:8.4
The limit in ERROR 1071 is in bytes, not characters. With utf8mb4 each character counts as up to 4 bytes, so a VARCHAR(1000) column is 4000 bytes as an index key. InnoDB allows 3072 bytes per key, and only 767 bytes when the table uses ROW_FORMAT=COMPACT (or REDUNDANT).
The number at the end of the message tells you which limit you hit. Both 3072 and 767 appeared in the tests below.
01Index on a long utf8mb4 VARCHAR column
pages.url is VARCHAR(1000) in utf8mb4. CREATE INDEX ix_pages_url ON pages (url) failed with max key length is 3072 bytes.
information_schema.columns showed CHARACTER_MAXIMUM_LENGTH 1000 and CHARACTER_OCTET_LENGTH 4000. A prefix index url(255) was created, and SHOW INDEX showed Sub_part 255.
Setup
CREATE TABLE pages (id INT PRIMARY KEY, url VARCHAR(1000)) DEFAULT CHARSET=utf8mb4;Query OK, 0 rows affected (0.01 sec)CREATE INDEX ix_pages_url ON pages (url);ERROR 1071 (42000): Specified key was too long; max key length is 3072 bytesSELECT column_name, character_maximum_length, character_octet_length FROM information_schema.columns WHERE table_schema = 'lab_a' AND table_name = 'pages' AND column_name = 'url';+-------------+--------------------------+------------------------+
| COLUMN_NAME | CHARACTER_MAXIMUM_LENGTH | CHARACTER_OCTET_LENGTH |
+-------------+--------------------------+------------------------+
| url | 1000 | 4000 |
+-------------+--------------------------+------------------------+
1 row in set (0.00 sec)A prefix index only indexes the first N characters, so it cannot be used as a UNIQUE check on the whole value. For uniqueness, shorten the column instead.
CREATE INDEX ix_pages_url ON pages (url(255));
SHOW INDEX FROM pages WHERE Key_name = 'ix_pages_url';Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
+-------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+-------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| pages | 1 | ix_pages_url | 1 | url | A | 0 | 255 | NULL | YES | BTREE | | | YES | NULL |
+-------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
1 row in set (0.00 sec)02A multi-column key adds up past 3072 bytes
city and street are each VARCHAR(500) utf8mb4 (2000 bytes each). Each column alone is under the limit, but ADD UNIQUE KEY uk_city_street (city, street) is 4000 bytes and failed with 1071.
The columns were shortened to VARCHAR(100) and VARCHAR(300) (1600 bytes together), and the same ADD UNIQUE KEY succeeded.
Setup
CREATE TABLE addresses (id INT PRIMARY KEY, city VARCHAR(500), street VARCHAR(500)) DEFAULT CHARSET=utf8mb4;Query OK, 0 rows affected (0.01 sec)ALTER TABLE addresses ADD UNIQUE KEY uk_city_street (city, street);ERROR 1071 (42000): Specified key was too long; max key length is 3072 bytesALTER TABLE addresses MODIFY city VARCHAR(100), MODIFY street VARCHAR(300);Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0ALTER TABLE addresses ADD UNIQUE KEY uk_city_street (city, street);Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 003ROW_FORMAT=COMPACT limits the key to 767 bytes
CREATE TABLE users (... email VARCHAR(255) NOT NULL UNIQUE) DEFAULT CHARSET=utf8mb4 ROW_FORMAT=COMPACT failed with max key length is 767 bytes. 255 characters × 4 bytes is 1020 bytes.
@@innodb_default_row_format was dynamic, so the limit came only from the explicit COMPACT. The same table with ROW_FORMAT=DYNAMIC was created and information_schema showed Dynamic.
CREATE TABLE users (id INT PRIMARY KEY, email VARCHAR(255) NOT NULL UNIQUE) DEFAULT CHARSET=utf8mb4 ROW_FORMAT=COMPACT;ERROR 1071 (42000): Specified key was too long; max key length is 767 bytesSELECT @@innodb_default_row_format;+-----------------------------+
| @@innodb_default_row_format |
+-----------------------------+
| dynamic |
+-----------------------------+
1 row in set (0.00 sec)If you see 767 instead of 3072, check the table or migration script for ROW_FORMAT=COMPACT or REDUNDANT.
CREATE TABLE users (id INT PRIMARY KEY, email VARCHAR(255) NOT NULL UNIQUE) DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC;
SELECT row_format FROM information_schema.tables WHERE table_schema = 'lab_a' AND table_name = 'users';Query OK, 0 rows affected (0.03 sec)
+------------+
| ROW_FORMAT |
+------------+
| Dynamic |
+------------+
1 row in set (0.00 sec)04CONVERT TO CHARACTER SET utf8mb4 on a utf8mb3 table
In utf8mb3 the indexed VARCHAR(1024) is 1024 × 3 = 3072 bytes, so the index fit. ALTER TABLE tags CONVERT TO CHARACTER SET utf8mb4 counts 4 bytes per character and fails with max key length is 3072 bytes.
Setup
CREATE TABLE tags (id INT PRIMARY KEY, name VARCHAR(1024), KEY ix_name (name)) DEFAULT CHARSET=utf8mb3;Query OK, 0 rows affected, 1 warning (0.01 sec)ALTER TABLE tags CONVERT TO CHARACTER SET utf8mb4;ERROR 1071 (42000): Specified key was too long; max key length is 3072 bytesALTER TABLE tags DROP INDEX ix_name, ADD INDEX ix_name (name(768));Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0Rebuilding the index as a prefix index name(768) (768 × 4 = 3072 bytes) first let the CONVERT succeed. The check query shows CHARACTER_SET_NAME utf8mb4 and CHARACTER_OCTET_LENGTH 4096.
ALTER TABLE tags CONVERT TO CHARACTER SET utf8mb4;
SELECT column_name, character_set_name, character_octet_length FROM information_schema.columns WHERE table_schema = 'lab_a' AND table_name = 'tags' AND column_name = 'name';Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
+-------------+--------------------+------------------------+
| COLUMN_NAME | CHARACTER_SET_NAME | CHARACTER_OCTET_LENGTH |
+-------------+--------------------+------------------------+
| name | utf8mb4 | 4096 |
+-------------+--------------------+------------------------+
1 row in set (0.00 sec)FAQ
Why does VARCHAR(255) give Specified key was too long?
With utf8mb4 it is 1020 bytes. That is fine under the 3072-byte limit, but in the test it failed when the table was ROW_FORMAT=COMPACT, where the limit is 767 bytes.
How do I fix max key length is 3072 bytes?
Make the key smaller in bytes: index a prefix like url(255), or shorten the columns. Both fixes worked in the tests.
Is the limit in characters or bytes?
Bytes. In the test, VARCHAR(1000) utf8mb4 had CHARACTER_OCTET_LENGTH 4000, which is over 3072.
Can a composite index cause ERROR 1071 even if each column is fine?
Yes. Two VARCHAR(500) utf8mb4 columns are 4000 bytes together, and the composite UNIQUE KEY failed even though each column alone was under 3072.
Why do I get Specified key was too long when converting to utf8mb4?
An index that fit at 3 bytes per character in utf8mb3 goes over 3072 bytes at 4 bytes per character. Rebuild it as a prefix index (for example name(768)) or shorten the column, then run the CONVERT.
How we verify
Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.
lab_a and lab_b are ordinary users created for the test, each with full rights on a database of the same name (its default database); root is the administrator. MySQL 8.4.13