16

innodb存储引擎 varchar类型字段 建立索引时 长度受限

 3 years ago
source link: https://zdb.im/archives/161.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

CREATE DATABASE jira CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;

给innodb数据表的varchar字段添加索引时, 不得超过 191字符;

解决:

MySQL的InnoDB存储引擎的表存在一系列的限制条件,其中比较常见的一种是表的字段索引长度限制,该限制与参数innodb_large_prefix相关。

问题现象

执行如下两个建表SQL操作,都返回了报错信息,导致建表失败:

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

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

原因分析

导致上面报错的原因是由于InnoDB表的索引长度限制,在MySQL5.6版本后引入了参数innodb_large_prefix可以解决这个问题。该参数控制是否允许单列的索引长度超过767字节,有ON和OFF两个取值:

ON :Innodb表的行记录格式是Dynamic或Compressed的前提下,单列索引长度上限扩展到3072个字节
OFF:Innodb表的单例索引长度最多为767个字节,索引长度超出后,主键索引会创建失败,辅助索引会被截断成为前缀索引

解决方案

  1. 使用前缀索引,减少字段索引长度至767 bytes,即varchar(191)
  2. 或者, 设置MySQL的全局参数innodb_large_prefix=ON,将InnoDB表的索引长度上限扩大到3072个字节

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK