mysql索引长度超过767bytes问题解决。

 

错误信息:

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

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

原因分析:

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

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

解决办法:

设置MySQL的全局参数innodb_large_prefix=ON,将InnoDB表的索引长度上限扩大到3072个字节。

MySQL5.6以及之前版本

  1. 以及之后的版本,不包括8.0,默认innodb_large_prefix为开启状态。
  2. 版本以及之后版本直接看到 4  ,5步骤

1. 查看MySQL数据库当前的配置;

show variables like 'innodb_large_prefix'; 
show variables like 'innodb_file_format';

 

 

2. 修改配置

set global innodb_large_prefix=ON;//开启不限制索引长度 
set global innodb_file_format=BARRACUDA;//这个不知道

 

3. 建表时需要指定ROW_FORMAT为dynamic或compressed

CREATE TABLE IMPORTS( 
`ID` int(10) NOT NULL AUTO_INCREMENT COMMENT '资产ID',
 PKG_ID int(10) NOT NULL COMMENT '资产ID',
 CLASS_NAME varchar(255) NOT NULL COMMENT '类名', 
) ENGINE=InnoDB ROW_FORMAT=dynamic DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

 

4. 表已经存在,只需修改。

ALTER TABLE `TableName ` row_format=dynamic;

 

5. 使用Navicat 15 可以直接设置行格式

右键表打开菜单--点击设计表--看到表设计窗口,点击选项--选择行格式--dynamic

 

现在就可以去添加索引了,

现在就可以去添加索引了~

现在就可以去添加索引了。

                               

MySQL5.6-摘自官方文档

innodb_large_prefix

Command-Line Format

--innodb-large-prefix[={OFF|ON}]

System Variable

innodb_large_prefix

Scope

Global

Dynamic

Yes

Type

Boolean

Default Value

OFF

Enable this option to allow index key prefixes longer than 767 bytes (up to 3072 bytes) for InnoDB tables that use DYNAMIC or COMPRESSED row format. (Creating such tables also requires the option values innodb_file_format=barracuda and innodb_file_per_table=true.) See Section 14.22, “InnoDB Limits” for maximums associated with index key prefixes under various settings.

For tables that use REDUNDANT or COMPACT row format, this option does not affect the permitted index key prefix length.

【机器翻译-仅供随便参考-切莫完全当真】

启用这个选项,对于使用动态或压缩行格式的InnoDB表,允许超过767字节(最多3072字节)的索引键前缀。

(创建这样的表还需要选项值innodb_file_format=barracuda 和 innodb_file_per_table=true。)

在不同的设置下,InnoDB限制与索引键前缀相关的最大值。

对于使用冗余或紧凑行格式的表,此选项不会影响允许的索引键前缀长度。

MySQL5.7-摘自官方文档

innodb_large_prefix

Command-Line Format

--innodb-large-prefix[={OFF|ON}]

Deprecated

Yes

System Variable

innodb_large_prefix

Scope

Global

Dynamic

Yes

Type

Boolean

Default Value

ON

When this option is enabled, index key prefixes longer than 767 bytes (up to 3072 bytes) are allowed for InnoDB tables that use DYNAMIC or COMPRESSED row format. See Section 14.23, “InnoDB Limits” for maximums associated with index key prefixes under various settings.

For tables that use REDUNDANT or COMPACT row format, this option does not affect the permitted index key prefix length.

innodb_large_prefix is enabled by default in MySQL 5.7. This change coincides with the default value change for innodb_file_format, which is set to Barracuda by default in MySQL 5.7. Together, these default value changes allow larger index key prefixes to be created when using DYNAMIC or COMPRESSED row format. If either option is set to a non-default value, index key prefixes larger than 767 bytes are silently truncated.

innodb_large_prefix is deprecated; expect it to be removed in a future release. innodb_large_prefix was introduced to disable large index key prefixes for compatibility with earlier versions of InnoDB that do not support large index key prefixes.

【机器翻译-仅供随便参考-切莫完全当真】

当启用该选项时,对于使用动态或压缩行格式的InnoDB表,允许超过767字节(最多3072字节)的索引键前缀。在不同的设置下,InnoDB限制与索引键前缀相关的最大值。

对于使用冗余或紧凑行格式的表,此选项不会影响允许的索引键前缀长度。

innodb_large_prefix在MySQL 5.7中默认开启。这个变化与innodb_file_format的默认值变化一致,MySQL 5.7默认设置为Barracuda。

在使用动态或压缩行格式时,这些默认值的更改允许创建更大的索引键前缀。

如果任一选项设置为非默认值,大于767字节的索引键前缀将被静默截断。

innodb_large_prefix弃用;希望它在未来的版本中被删除。innodb_large_prefix的引入是为了禁用大索引键前缀,以兼容早期版本的InnoDB不支持大索引键前缀。

参考以及摘取的文档:

https://blog.csdn.net/jian876601394/article/details/94313071

https://blog.csdn.net/baidu_27446515/article/details/106397298

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐