48

I know there have been plenty of questions about this, but I think my math is right.

  • MySQL reserves 3 bytes per UTF8 character.
  • MyISAM allows keys of length 1000 bytes.
  • My UTF8 VARCHAR(255) should be 255 * 3 = 765 bytes

Unless UNQUE requires an extra 200+ bytes per entry, why doesn't this work?

mysql> ALTER TABLE entry ADD UNIQUE INDEX (name(255));
ERROR 1071 (42000): Specified key was too long; max key length is 1000 bytes

Is there anything I can do about this?

EDIT:

It turns out the limit is 250. It seems chars count as 4 bytes for unique indices, but I don't know why.

EDIT 2:

Thanks Vladislav Vaintroub, the charset is indeed utf8mb4. That solves the mystery. I hadn't seen any documentation on this change.

I'm guessing it builds the non unique index by implicitly truncating the field, which is unacceptable for unique indices so it refuses.

If you re-enter your comment as an answer I'd be happy to accept it.

Solution: Specify utf8, not utf8mb4 (MySQL Admin doesn't allow this, so create the table manually)

5

3 Answers 3

48

If you're using utf8mb4, and you have unique indexes on varchar columns that are greater than 191 characters in length, you'll need to turn on innodb_large_prefix to allow for larger columns in indexes, because utf8mb4 requires more storage space than utf8 or latin1. Add the following to your my.cnf file.

[mysqld]
innodb_file_format=barracuda
innodb_file_per_table=1
innodb_large_prefix=1
init_connect='SET collation_connection = utf8mb4_unicode_ci; SET NAMES utf8mb4'
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci

More info about the why and future from MySQL 5.7 documentation:

If innodb_large_prefix is enabled (the default in MySQL 5.7.7), the index key prefix limit is 3072 bytes for InnoDB tables that use DYNAMIC or COMPRESSED row format. If innodb_large_prefix is disabled, the index key prefix limit is 767 bytes for tables of any row format.

innodb_large_prefix is deprecated in MySQL 5.7.7 and will be removed in a future release. innodb_large_prefix was introduced in MySQL 5.5 to disable large index key prefixes for compatibility with earlier versions of InnoDB that do not support large index key prefixes.

To sum up, the limit is only there for compatibility and will be increased in future versions.

3
  • I finally got this to work for me with importing a database dump after adding the line: default-storage-engine=InnoDB along with the above, inside the [mysqld] section. Feb 7, 2017 at 19:52
  • 2
    It's worth mentioning that the boolean entries in the config file must be set as 1 and not ON in MySQL 5.5, otherwise they are not taken into consideration. Also, since it is not possible to set a default row format in 5.5 and 5.6 (innodb_default_row_format system variable was added in 5.7.9), the tables must be created with ROW_FORMAT=DYNAMIC.
    – dregad
    Oct 11, 2017 at 8:07
  • Note that in the above suggested additions to config, the first init_connect is ignored or overwritten so the result is only "SET NAMES utf8mb4". If you want both in the init string, you might use init_connect = 'SET collation_connection = utf8mb4_unicode_ci; SET NAMES utf8mb4'
    – marcguyer
    Jan 5, 2018 at 15:56
9

MySQL reserves the max amount for a UTF8 field which is 4 bytes, so that is why you are blowing past the 1000 byte limit. My recommendation is to create the varchar at less than 255 or create it without UTF8.

Both of those solutions are probably not right for you or you would have already tried that.

The only other solution I can think of is to split the column into 2 small columns and create an unique index on both of those fields, but I believe that you would get the same error as above.

Since you probably need UTF8, I would seriously look at reducing the varchar(255) column down a little to 250 (or 249) to make this work.

2
  • 3
    I hate to nitpick, but a UTF-8-encoded character can be up to 6 bytes. Four bytes per character implies an internal UCS-4 (probably a C wchar_t-based) representation. (actually, I lied. I like to nitpick. Sorry!)
    – Alexios
    Dec 3, 2013 at 18:44
  • 5
    @Alexios Default utf8 encoding is 3 byte in MySQL, whereas utf8_mb4 is 4 byte variable (utf32 is 4 byte fixed)
    – Daniel W.
    Apr 7, 2014 at 8:06
8

Anyone who does need a larger key length should look at innodb_large_prefix

visit http://dev.mysql.com/doc/refman/5.5/en/innodb-parameters.html#sysvar_innodb_large_prefix

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.