首页 > 其他分享 >ERROR 1709 (HY000): Index column size too large. The maximum column size is 767 bytes.

ERROR 1709 (HY000): Index column size too large. The maximum column size is 767 bytes.

时间:2023-07-21 23:57:30浏览次数:67  
标签:Index varchar column utf8 prefix innodb file id size

MySQL版本5.6.35

在一个长度为512字符的字段上创建unique key报错

复制代码
CREATE DATABASE dpcs_metadata DEFAULT CHARACTER SET utf8;
select * from information_schema.SCHEMATA;
+--------------+--------------------+----------------------------+------------------------+----------+
| CATALOG_NAME | SCHEMA_NAME        | DEFAULT_CHARACTER_SET_NAME | DEFAULT_COLLATION_NAME | SQL_PATH |
+--------------+--------------------+----------------------------+------------------------+----------+
| def          | information_schema | utf8                       | utf8_general_ci        | NULL     |
| def          | dpcs_metadata      | utf8                       | utf8_general_ci        | NULL     |
| def          | mysql              | utf8                       | utf8_bin               | NULL     |
| def          | performance_schema | utf8                       | utf8_general_ci        | NULL     |
| def          | test               | utf8                       | utf8_bin               | NULL     |
+--------------+--------------------+----------------------------+------------------------+----------+
5 rows in set (0.00 sec)

use dpcs_metadata;

create table raw_log_meta_data(
       id bigint NOT NULL AUTO_INCREMENT,
       app_id varchar(64),
       user_id varchar(128),
       file_path varchar(512),
       device_id varchar(128),
       update_time DATETIME,
       PRIMARY KEY (id),
       UNIQUE KEY (user_id),
       UNIQUE KEY (file_path)
       ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes
复制代码

 MySQL用1到2个额外字节记录该字段的长度,当字段长度小于等于255时使用1个字节记录字段长度,当长度大于255时使用2~4个字节记录字段长度,字段file_path的长度为512*3+2=1538个字节,超过系统默认767字节数限制

复制代码
select * from information_schema.character_sets where character_set_name in('latin1', 'utf8', 'utf8mb4');
+--------------------+----------------------+----------------------+--------+
| CHARACTER_SET_NAME | DEFAULT_COLLATE_NAME | DESCRIPTION          | MAXLEN |
+--------------------+----------------------+----------------------+--------+
| latin1             | latin1_swedish_ci    | cp1252 West European |      1 |
| utf8               | utf8_general_ci      | UTF-8 Unicode        |      3 |
| utf8mb4            | utf8mb4_general_ci   | UTF-8 Unicode        |      4 |
+--------------------+----------------------+----------------------+--------+
3 rows in set (0.00 sec)
复制代码

根据上图所示,采用utf8编码的字段最大长度为255个字符时,255*3+1=756,是小于最大767限制的,可以创建成功,如下

复制代码
create table raw_log_meta_data(
       id bigint NOT NULL AUTO_INCREMENT,
       app_id varchar(64),
       user_id varchar(128),
       file_path varchar(256),
       device_id varchar(128),
       update_time DATETIME,
       PRIMARY KEY (id),
       UNIQUE KEY (user_id),
       UNIQUE KEY (file_path)
       ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes

create table raw_log_meta_data(
       id bigint NOT NULL AUTO_INCREMENT,
       app_id varchar(64),
       user_id varchar(128),
       file_path varchar(255),
       device_id varchar(128),
       update_time DATETIME,
       PRIMARY KEY (id),
       UNIQUE KEY (user_id),
       UNIQUE KEY (file_path)
       ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.19 sec)

desc raw_log_meta_data;
+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| id          | bigint(20)   | NO   | PRI | NULL    | auto_increment |
| app_id      | varchar(64)  | YES  |     | NULL    |                |
| user_id     | varchar(128) | YES  | UNI | NULL    |                |
| file_path   | varchar(255) | YES  | UNI | NULL    |                |
| device_id   | varchar(128) | YES  |     | NULL    |                |
| update_time | datetime     | YES  |     | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+
6 rows in set (0.01 sec)
复制代码

根据文档所述

https://dev.mysql.com/doc/refman/5.6/en/create-index.html

Prefix support and lengths of prefixes (where supported) are storage engine dependent. For example, a prefix can be up to 767 bytes long for InnoDB tables or 3072 bytes if the innodb_large_prefix option is enabled. For MyISAM tables, the prefix length limit is 1000 bytes. 

https://dev.mysql.com/doc/refman/5.6/en/innodb-parameters.html#sysvar_innodb_large_prefix

innodb_large_prefix

PropertyValue
Command-Line Format --innodb-large-prefix[={OFF|ON}]
Introduced 5.6.3
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.6.1.6, “Limits on InnoDB Tables” 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.

 https://dev.mysql.com/doc/refman/5.6/en/innodb-restrictions.html

By default, the index key prefix length limit is 767 bytes. See Section 13.1.13, “CREATE INDEX Syntax”. 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. When theinnodb_large_prefix configuration option is enabled, the index key prefix length limit is raised to 3072 bytes for InnoDB tables that use DYNAMIC or COMPRESSED row format.

根据以上可知,如果需要在长度大于255字符的字段上创建索引,需要修改以下3个参数

1. innodb_file_format=barracuda

2. innodb_file_per_table=true

3. ROW_FORMAT=DYNAMIC or COMPRESSED

复制代码
mysql> show variables like 'innodb_large_prefix';
+---------------------+-------+
| Variable_name       | Value |
+---------------------+-------+
| innodb_large_prefix | OFF   |
+---------------------+-------+
1 row in set (0.00 sec)

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

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

mysql> set global innodb_large_prefix='on';
Query OK, 0 rows affected (0.00 sec)

mysql> set global innodb_file_format='Barracuda';
Query OK, 0 rows affected (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> show variables like 'innodb_file_format';
+--------------------+-----------+
| Variable_name      | Value     |
+--------------------+-----------+
| innodb_file_format | Barracuda |
+--------------------+-----------+
1 row in set (0.00 sec)
复制代码 复制代码
create table raw_log_meta_data(
       id bigint NOT NULL AUTO_INCREMENT,
       app_id varchar(64),
       user_id varchar(128),
       file_path varchar(512),
       device_id varchar(128),
       update_time DATETIME,
       PRIMARY KEY (id),
       UNIQUE KEY (user_id),
       UNIQUE KEY (file_path)
       ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;
Query OK, 0 rows affected (0.29 sec)
复制代码

标签:Index,varchar,column,utf8,prefix,innodb,file,id,size
From: https://www.cnblogs.com/zzsdream/p/17572650.html

相关文章

  • Classical Management: emphasized rationality and making organizations and worker
    Classicalapproach:Firststudiesofmanagement,whichemphasized:rationalitymakingorganizationsandworkersasefficientaspossibleMaxWeber’sBureaucracy(OrganationalMachine)wasanattempttoformulatetheBureaucracyanidealprototypefororg......
  • Array方法: indexOf、filter、forEach、map、reduce详解
    [array方法:indexof、filter、foreach、map、reduce详解|FEblog](https://007sair.github.io/2015/08/17/js-Extras/#map)ECMAScript5标准新增了几个数组操作的方法,让我们来看看都是什么:Array.prototype.indexOfArray.prototype.lastIndexOfArray.prototype.everyArray.......
  • sizeof解析式分析
    sizeof类型:sizeof()是表达式不是函数用处:可以用来求变量、数组、对象、结构体等所占空间的大小,其实只要将sizeof()操作的参数理解成类型混淆点:sizeof()和数组        只有两种情况下数组名可以表示整个数组:(1)sizeof(数组名):此处数组名不能修饰,否则表示数组首元......
  • nova resize调整虚机资源大小
    1.创建新的资源模板nova flavor-list假如新的模板ID为222 2.查看虚机列表nova list 3.查看虚机详细信息nova show虚机ID(或者名字) 4.调整大小nova resize虚机ID   新模板ID 调整中: 调整完成后:   5.状态确认nova  resize-confirm 虚机ID......
  • Unity UGUI的ContentSizeFitter(内容尺寸适应器)组件的介绍及使用
    UnityUGUI的ContentSizeFitter(内容尺寸适应器)组件的介绍及使用1.什么是ContentSizeFitter组件?ContentSizeFitter是UnityUGUI中的一个组件,用于自动调整UI元素的大小,以适应其内容的大小变化。它可以根据内容的大小自动调整UI元素的宽度和高度,确保内容不会被截断或溢出。2.Cont......
  • Sqoop 数据导入hive size大小如何控制?
    描述:sqoop从hana导入数据到hive,数据量1300万,当设置参数-m7时,产生7个文件,但只有3个有数据,且大小不一,600多m,300dm,40m,修改参数为-m5,文件变成了5个,但是有数据的还是那三个,奇怪该如何控制文件大小接近块大小。 修改-m1时,倒是变成了一个1.04GB的文件修改-m2时,一......
  • @EnableRedisIndexedHttpSession
    使用@EnableRedisIndexedHttpSession增强SpringSession在Spring框架中,Session管理是Web应用开发中常见的需求之一。SpringSession是Spring提供的解决方案之一,它可以用于替代传统的Servlet容器提供的Session管理机制。SpringSession提供了一种使用不同的存储后端(如内存、Redis、......
  • html 学习 day3 css 的层叠 父子size 的继承
    今天遇到一个问题,css的父子继承关系在一种case下不生效。:子节点无法继承父节点的 width和height的设定下面代码的原始版本img无法继承div的宽和高的设定。当 img的css设定为:img{height:100%;width:100%;margin-right:1......
  • android transaction failed 29201/-1, size 0-0 line 3009
    解决"androidtransactionfailed29201/-1,size0-0line3009"错误引言在Android开发中,我们经常会遇到各种错误和异常。其中一个常见的错误是"androidtransactionfailed29201/-1,size0-0line3009"。这个错误通常与Fragment事务相关,并且可能会导致应用崩溃或功能异常......
  • docker nginx 配置/home/nginx/dist/index.html" is not found (2: No such file
    DockerNginx配置解析介绍Docker是一种开源的容器化平台,它可以轻松地打包、交付和运行应用程序。Nginx是一款高性能的Web服务器和反向代理服务器。本文将解析"DockerNginx配置/home/nginx/dist/index.html"isnotfound(2:Nosuchfile"错误,并给出相应的解决方案。错误解析......