Mysql5.7 varchar type field index failed unexpectedly

In the daily operation and maintenance of Mysql database, it is very common to create indexes on tables, but I have never thought about how long a single-column index can be. If the varchar type field is too long, will it fail to create a single-column index? Let's explore it together.

Check the official documents and found that the parameter innodb_large_prefix was introduced after MySQL 5.6. When it is enabled, the row record format of the Innodb table is Dynamic or Compressed, and the length of a single-column index can reach 3072 bytes. If it is not enabled With this parameter, the length of a single-column index cannot exceed 767 bytes in length.


Test environment introduction The

test environment is MySQL 5.7 version, and the default character set is utf8.

View innodb_large_prefix parameters

innodb_large_prefix参数在mysql5.7版本,默认是开启的。mysql> show variables like 'innodb_large%';
+---------------------+-------+
| Variable_name       | Value |
+---------------------+-------+
| innodb_large_prefix | ON    |
+---------------------+-------+
1 row in set (0.02 sec)

View the default record format of the test table
In the mysql5.7 version, the row record format of the Innodb table is Dynamic.


mysql> show variables like 'innodb_default_row_format';
+---------------------------+---------+
| Variable_name             | Value   |
+---------------------------+---------+
| innodb_default_row_format | dynamic |
+---------------------------+---------+
1 row in set (0.01 sec)

Simulate test
create test table and index

mysql> create table t_test2 (id int not null,name varchar(1024) not null default '',primary key(id));
Query OK, 0 rows affected (0.02 sec)

mysql> alter table t_test2 add index idx_t_test2_name(name);
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> create table t_test3 (id int not null,name varchar(1025) not null default '',primary key(id));
Query OK, 0 rows affected (0.03 sec)

mysql> alter table t_test3 add index idx_t_test3_name(name);
ERROR 1071 (42000): Specified key was too long; max key length is 3072

bytes can be seen from the test results, when the field length of varchar exceeds 1024, an error will be reported when creating a single-column index. Why? Because the character set of the database is utf8, a character length occupies 3 bytes, then the length of a single-column index It is 3072, converted into characters 3072 / 3=1024, so when the table is built, if the field length exceeds 1024, the index creation will fail.
Turn off the innodb_large_prefix parameter test

Turn off the innodb_large_prefix parameter


mysql> set global innodb_large_prefix=off;
Query OK, 0 rows affected, 1 warning (0.11 sec)

mysql> show variables like 'innodb_large_prefix';
+---------------------+-------+
| Variable_name       | Value |
+---------------------+-------+
| innodb_large_prefix | OFF   |
+---------------------+-------+
1 row in set (0.00 sec)

mysql> create table t_test4 (id int not null,name varchar(255) not null default '',primary key(id));
Query OK, 0 rows affected (0.02 sec)

mysql> alter table t_test4 add index idx_t_test4_name(name);
Query OK, 0 rows affected (0.35 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> create table t_test5 (id int not null,name varchar(256) not null default '',primary key(id));
Query OK, 0 rows affected (0.01 sec)

mysql> alter table t_test5 add index idx_t_test5_name(name);
ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes

It can be seen that at this time, if the field length exceeds 255, an error will be reported when creating a single-column index.

But in production, who would create an index on a column with a length of more than 1024? Isn't this sick, so it is recommended to turn off the innodb_large_prefix parameter to avoid creating indexes on long fields.

Guess you like

Origin blog.51cto.com/15061930/2642100