1071 - Specified key was too long; max key length is 767 bytes

今天优化公司项目时候创建索引时报了:
1071 - Specified key was too long; max key length is 767 bytes错误,意思就是“索引字段长度太长,超过了767bytes”。

查了一下 mysql的varchar主键只支持不超过767个字节或者768/2=384个双字节 或者767/3=255个三字节的字段 而GBK是双字节的,UTF8是三字节的。

innodb存储引擎,多列索引的长度限制如下:

  • 每个列的长度不能大于767 bytes;所有组成索引列的长度和不能大于3072 bytes

myisam存储引擎,多列索引长度限制如下:

  • 每个列的长度不能大于1000 bytes,所有组成索引列的长度和不能大于1000 bytes

定位到出错的建表语句,使用的字符集为(utf8mb4),而且长度是255(如下图所示)。
在这里插入图片描述
这个指每个字符最大的字节数为4,所以很明显 4*255 > 767
改变varchar的字符数,我改成了64就可以了。varchar(64)

问题虽然是解决了,但是难道InnoDB中单个索引字段的长度真的只能小于767?

又经过一番探索,发现在InnoDB中,可以启用启用innodb_large_prefix参数,来使得单个索引字段的长度突破767。

注意

  • 启用innodb_large_prefix参数能够取消对于索引中每列长度的限制(但是无法取消对于索引总长度的限制)
  • 启用innodb_large_prefix必须同时指定innodb_file_format=barracuda,innodb_file_per_table=true,并且建表的时候指定表的row_format为dynamic或者compressed(mysql 5.6中row_format默认值为compact)

启用innodb_large_prefix,那么限制值会增加到3072
具体的操作如下:

1 查看innodb_large_prefix,innodb_file_format参数

mysql> show variables like 'innodb_large_prefix';

+-------------------------+---------------+

| Variable_name | Value |

+--------------------------+--------------+

| innodb_large_prefix | OFF |

+--------------------------+--------------+

2 查看mysql> show variables like ‘innodb_file_format’;

+-------------------------+---------------+

| Variable_name | Value |

+-------------------------+---------------+

| innodb_file_format | Antelope |

+-------------------------+---------------+

3 建索引测试(innodb_large_prefix,innodb_file_format都为默认值的情况下)
在这里插入图片描述
并且索引列大于767 bytes时报错,组成索引列总长度大于3072 bytes时报错
④ 修改innodb_large_prefix,innodb_file_format参数

mysql> set global innodb_large_prefix=1;

mysql> set global innodb_file_format=BARRACUDA;

innodb_large_prefix=1并且innodb_file_format=BARRACUDA时,对于row_format为dynamic的表可以指定索引列长度大于767 bytes。但是索引列总长度的不能大于3072 bytes的限制仍然存在。

  • 5
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1071 - Specified key was too long; max key length is 767 bytes错误是指在创建索引时,索引字段的长度超过了数据库限制的最大长度767字节。这个错误可能会出现在不同的数据库系统中,比如MySQL。这个错误通常出现在创建唯一索引时,因为唯一索引需要保证索引字段的唯一性。如果你的索引字段过长,超过了数据库限制的最大长度,就会出现这个错误。解决这个问题的方法有几种。一种是缩短索引字段的长度,使其不超过最大长度限制。另一种是修改数据库的配置,增加最大索引长度的限制。还有一种是考虑使用前缀索引,将索引字段的前几个字符作为索引,以减少索引字段的长度。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [1071 - Specified key was too longmax key length is 767 bytes](https://blog.csdn.net/qq_38925911/article/details/118210392)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [MySQL错误“Specified key was too long; max key length is 1000 bytes”的解决办法](https://download.csdn.net/download/weixin_38606300/12835900)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [创建索引时,解决1071 Specified key was too longmax key length is 1000 bytes错误信息](https://blog.csdn.net/alpeace/article/details/116353774)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值