前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >MySQL故障案例 | ERROR 1071 (42000): Specified key was too long

MySQL故障案例 | ERROR 1071 (42000): Specified key was too long

原创
作者头像
凡人学运维
发布于 2022-06-22 15:09:36
发布于 2022-06-22 15:09:36
2.1K00
代码可运行
举报
运行总次数:0
代码可运行

MySQL 建表出现如下错误 (5.7)

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

查看官网内容得知:

If innodb_large_prefix is enabled (the default), the index key prefix limit is 3072 bytes for InnoDB tables that use the 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.

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

默认情况下单个列的索引不能超过767位,我们可以启用 innodb_large_prefix=on 选项,将约束项扩展至 3072 byte。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
# 1、在线开启
set global innodb_large_prefix = 1;

# 2、确认开启
show variables like 'innodb_large_prefix';

# 3、配置文件配置开启
innodb_large_prefix=on

另外,如果添加索引的字段长度过长,可以改为添加前缀索引的方式,如下

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
# 单列
alter table t1 add index idx_c1(c1(10));
 
# 多列
alter table t1 add index idx_c1_c2(c1(10),c2(10)); 

注意,添加前缀索引的长度并不是越长越好,这里涉及到一个选择性问题,

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
select 
    count(distinct 列名)/count(*)as a,
    COUNT(DISTINCT left(列名,100)) as b, 
    COUNT(DISTINCT left(列名,110)) as c 
from 表名;

END

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档