欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-11-08 23:44:10
今天在MySQL 5.6版本的数据库中修改InnoDB表字段长度时遇到了"ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes"错误,第一次遇到这个错误,遂花了点学习、研究过、总结这个问题。 我们先来创... ......

今天在mysql 5.6版本的数据库中修改innodb表字段长度时遇到了"error 1071 (42000): specified key was too long; max key length is 767 bytes"错误,第一次遇到这个错误,遂花了点学习、研究过、总结这个问题。

 

 

我们先来创建一个测试表,构造这样的错误。

 

mysql> use mydb;
reading table information for completion of table and column names
you can turn off this feature to get a quicker startup with -a
 
database changed
mysql> create table `test` (
    ->   `code_name` varchar(100) not null default '',
    ->   `code_seq` smallint(6) not null default '1',
    ->   `active` char(1) default 'y',
    ->   `code_value1` varchar(250) default null,
    ->   primary key (`code_name`,`code_seq`),
    ->   key `idx_gen_code` (`code_name`,`code_value1`)
    -> ) engine=innodb default charset=utf8;
query ok, 0 rows affected (0.02 sec)
 
 
mysql> alter table test modify code_value1 varchar(350);
error 1071 (42000): specified key was too long; max key length is 767 bytes
mysql> 

 

   其实这个error 1071 (42000): specified key was too long; max key length is 767 bytes错误是指超出索引字节的限制,并不是指字段长度限制。在官方文档limits on innodb tables有关于这方面的介绍、描述(详情请见参考资料):

 

 

mysql 5.6文档内容如下

 

 

by default, the index key prefix length limit is 767 bytes. see section 13.1.13, “create index syntax”. for example, you might hit this limit with a column prefix index of more than 255 characters on a text or varchar column, assuming a utf8mb3 character set and the maximum of 3 bytes for each character. when the innodb_large_prefix configuration option is enabled, the index key prefix length limit is raised to 3072 bytes for innodb tables that use dynamic or compressed row format.

 

attempting to use an index key prefix length that exceeds the limit returns an error. to avoid such errors in replication configurations, avoid enablinginnodb_large_prefix on the master if it cannot also be enabled on slaves.

the limits that apply to index key prefixes also apply to full-column index keys.

 

 

mysql 5.7文档内容如下

 

if innodb_large_prefix is enabled (the default), 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 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.

the index key prefix length limit is 767 bytes for innodb tables that use the redundant or compact row format. for example, you might hit this limit with a column prefix index of more than 255 characters on a text or varchar column, assuming a utf8mb3 character set and the maximum of 3 bytes for each character.

attempting to use an index key prefix length that exceeds the limit returns an error. to avoid such errors in replication configurations, avoid enablinginnodb_large_prefix on the master if it cannot also be enabled on slaves.

the limits that apply to index key prefixes also apply to full-column index keys.

 

 

如果启用了系统变量innodb_large_prefix(默认启用,注意实验版本为mysql  5.6.41,默认是关闭的,mysql 5.7默认开启),则对于使用dynamic或compressed行格式的innodb表,索引键前缀限制为3072字节。如果禁用innodb_large_prefix,则对于任何行格式的表,索引键前缀限制为767字节。

 

innodb_large_prefix将在以后的版本中删除、弃用。在mysql 5.5中引入了innodb_large_prefix,用来禁用大型前缀索引,以便与不支持大索引键前缀的早期版本的innodb兼容。

对于使用redundant或compact行格式的innodb表,索引键前缀长度限制为767字节。例如,您可能会在text或varchar列上使用超过255个字符的列前缀索引达到此限制,假设为utf8mb3字符集,并且每个字符最多包含3个字节。

 

尝试使用超出限制的索引键前缀长度会返回错误。要避免复制配置中出现此类错误,请避免在主服务器上启用enableinnodb_large_prefix(如果无法在从服务器上启用)。

 

适用于索引键前缀的限制也适用于全列索引键。

 

 

注意:上面是767个字节,而不是字符,具体到字符数量,这就跟字符集有关。gbk是双字节的,utf-8是三字节的

 

 

解决方案:

 

 

1:启用系统变量innodb_large_prefix

 

注意:光有这个系统变量开启是不够的。必须满足下面几个条件:

 

    1: 系统变量innodb_large_prefix为on

    2: 系统变量innodb_file_format为barracuda

    3: row_format为dynamic或compressed

 

如下测试所示:

 

mysql> show variables like '%innodb_large_prefix%';
+---------------------+-------+
| variable_name       | value |
+---------------------+-------+
| innodb_large_prefix | off   |
+---------------------+-------+
1 row in set (0.00 sec)
 
mysql> set global innodb_large_prefix=on;
query ok, 0 rows affected (0.00 sec)
 
mysql> alter table test modify code_value1 varchar(350);
error 1709 (hy000): index column size too large. the maximum column size is 767 bytes.
mysql> 
mysql> show variables like '%innodb_file_format%';
+--------------------------+-----------+
| variable_name            | value     |
+--------------------------+-----------+
| innodb_file_format       | antelope  |
| innodb_file_format_check | on        |
| innodb_file_format_max   | barracuda |
+--------------------------+-----------+
3 rows in set (0.01 sec)
 
mysql> set global innodb_file_format=barracuda;
query ok, 0 rows affected (0.00 sec)
 
mysql> alter table test modify code_value1 varchar(350);
error 1709 (hy000): index column size too large. the maximum column size is 767 bytes.
mysql> 
 
mysql> 
mysql> show table status from mydb where name='test'\g;
*************************** 1. row ***************************
           name: test
         engine: innodb
        version: 10
     row_format: compact
           rows: 0
 avg_row_length: 0
    data_length: 16384
max_data_length: 0
   index_length: 16384
      data_free: 0
 auto_increment: null
    create_time: 2018-09-20 13:53:49
    update_time: null
     check_time: null
      collation: utf8_general_ci
       checksum: null
 create_options: 
        comment: 
 
mysql>  alter table test row_format=dynamic;
query ok, 0 rows affected (0.05 sec)
records: 0  duplicates: 0  warnings: 0
 
mysql> show table status from mydb where name='test'\g;
*************************** 1. row ***************************
           name: test
         engine: innodb
        version: 10
     row_format: dynamic
           rows: 0
 avg_row_length: 0
    data_length: 16384
max_data_length: 0
   index_length: 16384
      data_free: 0
 auto_increment: null
    create_time: 2018-09-20 14:04:05
    update_time: null
     check_time: null
      collation: utf8_general_ci
       checksum: null
 create_options: row_format=dynamic
        comment: 
1 row in set (0.00 sec)
 
error: 
no query specified
 
mysql> alter table test modify code_value1 varchar(350);
query ok, 0 rows affected (0.02 sec)
records: 0  duplicates: 0  warnings: 0

 

 

 

 

2:使用前缀索引解决这个问题

 

 

之所以要限制索引键值的大小,是因为性能问题,而前缀索引能很好的解决这个问题。不需要修改任何系统变量。

 

 

mysql> show index from test;
..................................
 
mysql> alter table test drop index idx_gen_code;
query ok, 0 rows affected (0.00 sec)
records: 0  duplicates: 0  warnings: 0
 
mysql> create idx_gen_code test on test (code_name, code_value1(12));
query ok, 0 rows affected (0.01 sec)
records: 0  duplicates: 0  warnings: 0
 
mysql> alter table test modify code_value1 varchar(350);
query ok, 1064 rows affected (0.08 sec)
records: 1064  duplicates: 0  warnings: 0

 

 

问题延伸: 为什么innodb的索引字节数限制为767字节? 而不是800字节呢? 这样限制又是出于什么具体性能的考虑呢? 暂时还没有弄清楚这些细节问题!

 

 

 

 

 

参考资料:

 

https://dev.mysql.com/doc/refman/5.6/en/innodb-restrictions.html

https://dev.mysql.com/doc/refman/5.7/en/innodb-restrictions.html