MySQL Index Key Length Calculator
Paste CREATE TABLE, CREATE INDEX or ALTER TABLE ... ADD INDEX statements and see how many bytes each InnoDB index needs. The calculator resolves the character set of every column (column CHARACTER SET or COLLATE, then the table default), multiplies VARCHAR and CHAR lengths by the maximum bytes per character (4 for utf8mb4, 3 for utf8mb3, 1 for latin1 and binary), sizes numeric and temporal columns, and compares the result with the 3072-byte DYNAMIC limit or the 767-byte COMPACT/REDUNDANT limit. It predicts ERROR 1071 (Specified key was too long), 1170 (BLOB/TEXT column without a key length), 1089 (incorrect prefix key) and 1171, shows when non-strict SQL mode silently shortens an index to a prefix, and reports the key_len EXPLAIN shows when the whole index is used. Everything runs in your browser.
Do more than mysql index key length calculator — meet Chat2DB
Chat2DB is an AI-powered SQL client for Windows, macOS and Linux. Write SQL in natural language, format and optimize queries automatically, and manage MySQL, PostgreSQL, Oracle and 20+ other databases in one workspace.
How to use
- Paste your CREATE TABLE statement and any CREATE INDEX or ALTER TABLE ADD INDEX statements.
- Pick the server default character set, innodb_page_size and whether strict SQL mode is on, then click Analyze.
- Read the byte count of each key part, the verdict for every index, the EXPLAIN key_len, and the suggested fix for any error.
Frequently asked questions
Why is VARCHAR(255) too long for an index in MySQL?
Index limits are in bytes, not characters, and InnoDB reserves the maximum bytes per character. With utf8mb4 a VARCHAR(255) needs 1020 bytes, which exceeds the 767-byte limit of the COMPACT and REDUNDANT row formats. With the default DYNAMIC row format the limit is 3072 bytes, so up to VARCHAR(768) utf8mb4 fits in a single-column index. That is why older frameworks used VARCHAR(191): 191 × 4 = 764 bytes.
How is EXPLAIN key_len calculated?
For each key part used, key_len adds the column's byte length (characters × bytes per character for strings, the storage size for numbers and dates), plus 1 byte if the column is nullable and 2 bytes for the length of VARCHAR, VARBINARY, TEXT and BLOB values. A NOT NULL INT is 4, a nullable VARCHAR(50) utf8mb4 is 203. Comparing key_len with the full value tells you how many columns of a composite index a query really uses.
How do I fix 'Specified key was too long; max key length is 3072 bytes'?
Shorten the column, index a prefix such as KEY (url(255)), move long values to a generated hash column with a UNIQUE index, use a smaller character set for ASCII-only data such as codes and hashes, or switch COMPACT tables to ROW_FORMAT=DYNAMIC when the limit is 767 bytes. Chat2DB can inspect your MySQL indexes and generate the ALTER TABLE statements with its AI assistant: download it at chat2db.ai/download or use app.chat2db.ai in the browser.
