今天在MySQL5.6版本的数据库中修改InnoDB表字段长度时遇到了"ERROR1071(42000):Specifiedkeywastoolong;maxkeylengthis767bytes"错误,第一次遇到这个错误,遂花了点学习、研究过、总结这个问题。
我们先来创建一个测试表,构造这样的错误。
mysql>useMyDB;
Readingtableinformationforcompletionoftableandcolumnnames
Youcanturnoffthisfeaturetogetaquickerstartupwith-A
Databasechanged
mysql>CREATETABLE`TEST`(
->`CODE_NAME`varchar(100)NOTNULLDEFAULT'',
->`CODE_SEQ`smallint(6)NOTNULLDEFAULT'1',
->`ACTIVE`char(1)DEFAULT'Y',
->`CODE_VALUE1`varchar(250)DEFAULTNULL,
->PRIMARYKEY(`CODE_NAME`,`CODE_SEQ`),
->KEY`IDX_GEN_CODE`(`CODE_NAME`,`CODE_VALUE1`)
->)ENGINE=InnoDBDEFAULTCHARSET=utf8;
QueryOK,0rowsaffected(0.02sec)
mysql>ALTERTABLETESTMODIFYCODE_VALUE1VARCHAR(350);
ERROR1071(42000):Specifiedkeywastoolong;maxkeylengthis767bytes
mysql>
其实这个“ERROR1071(42000):Specifiedkeywastoolong;maxkeylengthis767bytes”错误是指超出索引字节的限制,并不是指字段长度限制。在官方文档“LimitsonInnoDBTables”有关于这方面的介绍、描述(详情请见参考资料):
MySQL5.6文档内容如下
Bydefault,theindexkeyprefixlengthlimitis767bytes.SeeSection13.1.13,“CREATEINDEXSyntax”.Forexample,youmighthitthislimitwithacolumnprefixindexofmorethan255charactersonaTEXTorVARCHARcolumn,assumingautf8mb3charactersetandthemaximumof3bytesforeachcharacter.Whentheinnodb_large_prefixconfigurationoptionisenabled,theindexkeyprefixlengthlimitisraisedto3072bytesforInnoDBtablesthatuseDYNAMICorCOMPRESSEDrowformat.
Attemptingtouseanindexkeyprefixlengththatexceedsthelimitreturnsanerror.Toavoidsucherrorsinreplicationconfigurations,avoidenablinginnodb_large_prefixonthemasterifitcannotalsobeenabledonslaves.
Thelimitsthatapplytoindexkeyprefixesalsoapplytofull-columnindexkeys.
MySQL5.7文档内容如下:
Ifinnodb_large_prefixisenabled(thedefault),theindexkeyprefixlimitis3072bytesforInnoDBtablesthatuseDYNAMICorCOMPRESSEDrowformat.Ifinnodb_large_prefixisdisabled,theindexkeyprefixlimitis767bytesfortablesofanyrowformat.
innodb_large_prefixisdeprecatedandwillberemovedinafuturerelease.innodb_large_prefixwasintroducedinMySQL5.5todisablelargeindexkeyprefixesforcompatibilitywithearlierversionsofInnoDBthatdonotsupportlargeindexkeyprefixes.
Theindexkeyprefixlengthlimitis767bytesforInnoDBtablesthatusetheREDUNDANTorCOMPACTrowformat.Forexample,youmighthitthislimitwithacolumnprefixindexofmorethan255charactersonaTEXTorVARCHARcolumn,assumingautf8mb3charactersetandthemaximumof3bytesforeachcharacter.
Attemptingtouseanindexkeyprefixlengththatexceedsthelimitreturnsanerror.Toavoidsucherrorsinreplicationconfigurations,avoidenablinginnodb_large_prefixonthemasterifitcannotalsobeenabledonslaves.
Thelimitsthatapplytoindexkeyprefixesalsoapplytofull-columnindexkeys.
如果启用了系统变量innodb_large_prefix(默认启用,注意实验版本为MySQL5.6.41,默认是关闭的,MySQL5.7默认开启),则对于使用DYNAMIC或COMPRESSED行格式的InnoDB表,索引键前缀限制为3072字节。如果禁用innodb_large_prefix,则对于任何行格式的表,索引键前缀限制为767字节。
innodb_large_prefix将在以后的版本中删除、弃用。在MySQL5.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>showvariableslike'%innodb_large_prefix%';
+---------------------+-------+
|Variable_name|Value|
+---------------------+-------+
|innodb_large_prefix|OFF|
+---------------------+-------+
1rowinset(0.00sec)
mysql>setglobalinnodb_large_prefix=on;
QueryOK,0rowsaffected(0.00sec)
mysql>ALTERTABLETESTMODIFYCODE_VALUE1VARCHAR(350);
ERROR1709(HY000):Indexcolumnsizetoolarge.Themaximumcolumnsizeis767bytes.
mysql>
mysql>showvariableslike'%innodb_file_format%';
+--------------------------+-----------+
|Variable_name|Value|
+--------------------------+-----------+
|innodb_file_format|Antelope|
|innodb_file_format_check|ON|
|innodb_file_format_max|Barracuda|
+--------------------------+-----------+
3rowsinset(0.01sec)
mysql>setglobalinnodb_file_format=Barracuda;
QueryOK,0rowsaffected(0.00sec)
mysql>ALTERTABLETESTMODIFYCODE_VALUE1VARCHAR(350);
ERROR1709(HY000):Indexcolumnsizetoolarge.Themaximumcolumnsizeis767bytes.
mysql>
mysql>
mysql>showtablestatusfromMyDBwherename='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-2013:53:49
Update_time:NULL
Check_time:NULL
Collation:utf8_general_ci
Checksum:NULL
Create_options:
Comment:
mysql>ALTERTABLETESTROW_FORMAT=DYNAMIC;
QueryOK,0rowsaffected(0.05sec)
Records:0Duplicates:0Warnings:0
mysql>showtablestatusfromMyDBwherename='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-2014:04:05
Update_time:NULL
Check_time:NULL
Collation:utf8_general_ci
Checksum:NULL
Create_options:row_format=DYNAMIC
Comment:
1rowinset(0.00sec)
ERROR:
Noqueryspecified
mysql>ALTERTABLETESTMODIFYCODE_VALUE1VARCHAR(350);
QueryOK,0rowsaffected(0.02sec)
Records:0Duplicates:0Warnings:0
clip_image001
2:使用前缀索引解决这个问题
之所以要限制索引键值的大小,是因为性能问题,而前缀索引能很好的解决这个问题。不需要修改任何系统变量。
mysql>showindexfromTEST;
..................................
mysql>ALTERTABLETESTDROPINDEXIDX_GEN_CODE;
QueryOK,0rowsaffected(0.00sec)
Records:0Duplicates:0Warnings:0
mysql>CREATEIDX_GEN_CODETESTONTEST(CODE_NAME,CODE_VALUE1(12));
QueryOK,0rowsaffected(0.01sec)
Records:0Duplicates:0Warnings:0
mysql>ALTERTABLETESTMODIFYCODE_VALUE1VARCHAR(350);
QueryOK,1064rowsaffected(0.08sec)
Records:1064Duplicates:0Warnings:0
问题延伸:为什么InnoDB的索引字节数限制为767字节?而不是800字节呢?这样限制又是出于什么具体性能的考虑呢?暂时还没有弄清楚这些细节问题!
转子:https://www.cnblogs.com/kerrycode/p/9680881.html