What should I do if #1071 - specified key was too long; max key length is 767 bytes?

611    Asked by GarethBeacham in SQL Server , Asked on Oct 3, 2022

Our server is creating a lot of tables and one of them is the Lock_ table.

We are using MySQL 5.5 on Percona. Also we are using utf8_general_ci and utf8.

It throws the following error when creating the following index on Lock_ table:

ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes

Here is the create-table query:

create table Lock_ (
    uuid_ VARCHAR(75) null,
    lockId LONG not null primary key,
    companyId LONG,
    userId LONG,
    userName VARCHAR(75) null,
    createDate DATE null,
    className VARCHAR(75) null,
    key_ VARCHAR(200) null,
    owner VARCHAR(255) null,
    inheritable BOOLEAN,
    expirationDate DATE null
);

Index query:

create unique index IX_DD635956 on Lock_ (className, key_, owner);

This problem is happening on one of our prod servers (previously it was standalone mysql and then later moved to percona, but the problem was present in both the editions), but this is working fine on our dev environment.


So is there a way we can update the prod environment's index length or something so that it does not throw this error?

Answered by Gayatri Jaiteley

If #1071 - specified key was too long; max key length is 767 bytes -


I suspect the difference is a Unicode length issue. As you are not using a single-byte-per-character collation the total length of the fields in your key is 530 character (75+200+255) and not 530 bytes, and 530 characters could easily be more than 767 bytes depending on what language you are storing in those strings (in fact definitely will be if mysql stores unicode data always as two-bytes-per-character as MS SQL Server does in its NVARCHAR column type).

As to why it is working in Dev but not live:

If mysql doesn't use a fixed length encoding then it could be that existing data in some rows in production is long enough to break the 767 byte limit, but in Dev there are no such rows. The length() function (http://dev.mysql.com/doc/refman/5.1/en/string-functions.html#function_length) returns the number of bytes, not characters, so you can scan for such rows by selecting where LENGTH(className)+LENGTH(key)+LENGTH(owner) > 767.

Otherwise it could be that your collation settings are not identical between dev and production, try verifying those.



Your Answer

Interviews

Parent Categories