博客
关于我
MySQL创建索引时提示“Specified key was too long; max key length is 767 bytes”
阅读量:803 次
发布时间:2023-02-12

本文共 868 字,大约阅读时间需要 2 分钟。

在使用MySQL时,创建索引时出现“Specified key was too long; max key length is 767 bytes”错误,通常是由于索引字段的长度超过了InnoDB引擎的限制。以下是解决方案:

问题分析

  • 错误背景:InnoDB引擎对索引字段的最大长度限制为767字节,这在使用多字节字符集(如utf8mb4)时尤为明显,因为每个字符占用4字节,767/4=191字符。因此,定义超过191字符的字段(如varchar(255)或char(255))创建索引时会失败。
  • 原因根源:MySQL 8.0已移除innodb_large_prefix参数,现仅支持通过修改表的Row_format来解决。

解决方案

1. 配置InnoDB引擎参数

  • 注意:由于innodb_large_prefix已被移除,请直接跳过此步骤。

2. 修改表的Row_format

  • 步骤1:登录数据库,执行以下SQL语句查看目标表的Row_format:

    SHOW TABLE STATUS LIKE 'TableName'\G

    检查输出中Current_row_format的值,确认是否为DYNAMIC或COMPRESSED。

  • 步骤2:若当前Row_format不是DYNAMIC或COMPRESSED,执行以下命令修改:

    ALTER TABLE TableName Row_format=DYNAMIC;

    ALTER TABLE TableName Row_format=COMPRESSED;

    注意:在修改Row_format前,确保表未被使用或已备份,以避免潜在数据丢失风险。

优化建议

  • 性能考量:DYNAMIC和COMPRESSED格式的表在某些情况下可能影响查询性能和存储效率,请根据业务需求选择合适的Row_format。
  • 字段长度控制:确保索引字段长度不超过3072字节,避免再次触发错误。

通过以上步骤,应该能够成功创建所需索引。如果问题依旧,建议参考MySQL官方文档或咨询技术支持,确保每一步操作的正确性。

转载地址:http://elbfk.baihongyu.com/

你可能感兴趣的文章
NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty()
查看>>
NotImplementedError: Could not run torchvision::nms
查看>>
nova基于ubs机制扩展scheduler-filter
查看>>
Now trying to drop the old temporary tablespace, the session hangs.
查看>>
nowcoder—Beauty of Trees
查看>>
np.arange()和np.linspace()绘制logistic回归图像时得到不同的结果?
查看>>
np.power的使用
查看>>
NPM 2FA双重认证的设置方法
查看>>
npm build报错Cannot find module ‘webpack/lib/rules/BasicEffectRulePlugin‘解决方法
查看>>
npm build报错Cannot find module ‘webpack‘解决方法
查看>>
npm ERR! ERESOLVE could not resolve报错
查看>>
npm ERR! fatal: unable to connect to github.com:
查看>>
npm ERR! Unexpected end of JSON input while parsing near '...on":"0.10.3","direc to'
查看>>
npm ERR! Unexpected end of JSON input while parsing near ‘...“:“^1.2.0“,“vue-html-‘ npm ERR! A comp
查看>>
npm error Missing script: “server“npm errornpm error Did you mean this?npm error npm run serve
查看>>
npm error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装
查看>>
npm install CERT_HAS_EXPIRED解决方法
查看>>
npm install digital envelope routines::unsupported解决方法
查看>>
npm install 卡着不动的解决方法
查看>>
npm install 报错 EEXIST File exists 的解决方法
查看>>