签到成功

知道了

CNDBA社区CNDBA社区

MySQL创建索引报错Index column size too large 说明

2024-06-28 20:24 665 0 原创 MySQL
作者: dave

业务侧反馈说在 mysql 5.7 中建表的时候报了如下错误:

http://www.cndba.cn/dave/article/131559
http://www.cndba.cn/dave/article/131559

ERROR 1709 (HY000): Index column size too large. The maximum column size is 767 bytes.http://www.cndba.cn/dave/article/131559

这个是 5.6 的遗留问题,在 5.7 中已经解决了。但业务说的那么确认,还是重新验证了一下,将业务的 SQL 拿到 5.7 的环境中执行,确实没有这个问题。

mysql> select @@version;
+---------------+
| @@version     |
+---------------+
| 5.7.44-48-log |
+---------------+
1 row in set (0.00 sec)

mysql> CREATE TABLE ustc (
    ->   `id` bigint(20) ,
    ->   `url` varchar(255),
    ->   INDEX idx_config_key(url) USING BTREE
    -> )  ROW_FORMAT=DYNAMIC CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ;
Query OK, 0 rows affected (0.01 sec)

8.4 官网的说明:

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

The index key prefix length limit is 3072 bytes for InnoDB tables that use DYNAMIC or COMPRESSED row format.
The index key prefix length limit is 767 bytes for InnoDB tables that use the REDUNDANT or COMPACT row format. For example, you might hit this limit with a column prefix index of more than 191 characters on a TEXT or VARCHAR column, assuming a utf8mb4 character set and the maximum of 4 bytes for each character.

http://www.cndba.cn/dave/article/131559
http://www.cndba.cn/dave/article/131559

5.7 官网的说明:

https://dev.mysql.com/doc/refman/5.7/en/innodb-limits.html
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.
innodb_large_prefix is deprecated; expect it to be removed in a future MySQL release. innodb_large_prefix was introduced in MySQL 5.5 to disable large index key prefixes for compatibility with earlier versions of InnoDB that do not support large index key prefixes.
The index key prefix length limit is 767 bytes for InnoDB tables that use the REDUNDANT or COMPACT row format. For example, you might hit this limit with a column prefix index of more than 255 characters on a TEXT or VARCHAR column, assuming a utf8mb3 character set and the maximum of 3 bytes for each character.http://www.cndba.cn/dave/article/131559http://www.cndba.cn/dave/article/131559

可以看到,到了 5.7 以后的版本,在innodb_large_prefix 参数启用(默认值),并且 row format使用DYNAMIC 或 COMPRESSED的情况下,index key prefix 的限制是 3072. 只有innodb_large_prefix 禁用的情况下(5.6),index key prefix 才是 767 bytes。

如果表的字符集是utf8mb4,默认支持的索引字段最大长度是191字符(767字节/4字节每字符≈191字符),因此在varchar(255)或char(255)类型字段上创建索引会失败。

在 MySQL 5.7 中,参数默认就是支持建立长字节索引的:

http://www.cndba.cn/dave/article/131559

mysql> show variables like '%row_format%';
+---------------------------+---------+
| Variable_name             | Value   |
+---------------------------+---------+
| innodb_default_row_format | dynamic |
+---------------------------+---------+
1 row in set (0.00 sec)

mysql> show variables like 'innodb_large_prefix';
+---------------------+-------+
| Variable_name       | Value |
+---------------------+-------+
| innodb_large_prefix | ON    |
+---------------------+-------+
1 row in set (0.00 sec)

MySQL 5.6 的参数 innodb_large_prefix 默认为 off ,所以对于 5.6 的环境,可以考虑启用innodb_large_prefix,然后在 create/alert table 时加上ROW_FORMAT=DYNAMIC。

http://www.cndba.cn/dave/article/131559
http://www.cndba.cn/dave/article/131559

mysql> set GLOBAL innodb_large_prefix=ON;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> set global innodb_default_row_format = dynamic;
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TABLE ustc3 (
    ->   `id` bigint(20) ,
    ->   `url` varchar(255),
    ->   INDEX idx_config_key(url) USING BTREE
    -> ) ROW_FORMAT=DYNAMIC CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ;
Query OK, 0 rows affected (0.00 sec)

版权声明:本文为博主原创文章,未经博主允许不得转载。

用户评论
* 以下用户言论只代表其个人观点,不代表CNDBA社区的观点或立场
dave

dave

关注

人的一生应该是这样度过的:当他回首往事的时候,他不会因为虚度年华而悔恨,也不会因为碌碌无为而羞耻;这样,在临死的时候,他就能够说:“我的整个生命和全部精力,都已经献给世界上最壮丽的事业....."

  • 2261
    原创
  • 3
    翻译
  • 578
    转载
  • 191
    评论
  • 访问:7966231次
  • 积分:4346
  • 等级:核心会员
  • 排名:第1名
精华文章
    最新问题
    查看更多+
    热门文章
      热门用户
      推荐用户
        Copyright © 2016 All Rights Reserved. Powered by CNDBA · 皖ICP备2022006297号-1·

        QQ交流群

        注册联系QQ