Using Innodb_large_prefix to Avoid ERROR 1071
If you’ve ever tried to add an index that includes a long varchar column to an InnoDB table in MySQL, you may have seen this error:
The character limit depends on the character set you use. For example if you use
latin1 then the largest column you can index is
varchar(767), but if you use
utf8 then the limit is
varchar(255). There is also a separate 3072 byte limit per index. The 767 byte limit is per column, so you can include multiple columns (each 767 bytes or smaller) up to 3072 total bytes per index, but no column longer than 767 bytes. (MyISAM is a little different. It has a 1000 byte index length limit, but no separate column length limit within that).
One workaround for these limits is to only index a prefix of the longer columns, but what if you want to index more than 767 bytes of a column in InnoDB?
In that case you should consider using
innodb_large_prefix, which was introduced in MySQL 5.5.14 and allows you to include columns up to 3072 bytes long in InnoDB indexes. It does not affect the index limit, which is still 3072 bytes as quoted in the manual:
The InnoDB internal maximum key length is 3500 bytes, but MySQL itself restricts this to 3072 bytes. This limit applies to the length of the combined index key in a multi-column index.
Read on for details and examples about
Here are a few pre-requisites for using
- At the database level you have to use
- At the table level you have to use
The default file format is still Antelope for backwards compatibility, and the default row format is COMPACT.
You can set both
innodb_large_prefix dynamically, but you should also set them in my.cnf so they survive a restart.
Here’s an example. If I try to create this table with
innodb_large_prefix disabled I get an error:
1 2 3 4 5 6 7 8
If I enable
innodb_large_prefix I can create the table successfully:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
The examples are similar for
latin1, but I can use columns three times as long since it’s a single-byte character set.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
And here’s what happens if I try to create an index longer than 3072 bytes:
1 2 3 4 5 6 7 8 9