0

关于MySQL参数innodb_large_prefix设置的问题

 2 years ago
source link: http://www.veiking.cn/blog/1068-page.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.

关于MySQL参数innodb_large_prefix设置的问题

程序员甲   @Veiking   2021-04-05

在低版本的MySQL使用过程中,当我们在使用某个较长字段创建唯一索引时,经常会爆出1709的错误:innodb_large_prefix and ERROR 1709 (HY000): Index column size too large. The maximum column size is 767 bytes. 这是设置索引长度超了。报这个错误在5.6之前的MySQL使用中非常普遍,但不同版本处理的方法多少还是有些不同,需要注意

低版本MySQL使用过程中,当我们在使用某个较长字段,去尝试创建唯一索引时,经常会爆出1709的错误:

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

从字面的意思可以知道,是索引长度超了,超出了767这个长度的限制,一般默认情况下,在varchar字段上做索引操作,怎么说都会超的。

报这个错误在5.6之前的MySQL使用中非常普遍,但各个版本处理的方法多少还是有些不同。

在5.6中,普遍的应对方法就是设置两项参数:

set global innodb_file_format = BARRACUDA;
set global innodb_large_prefix = ON;

或者在数据库配置文件 my.ini(my.cnf) 中作如下配置:

innodb-file-format=barracuda
innodb-large-prefix=ON

没什么意外的话重启服务器就OK了,但如果还是不行,这就得留意下innodb-file-per-table参数是否有问题,同时操作如下:

set global innodb_file_per_table = ON;
innodb_file_per_table=ON

这样基本就没什么问题了。


但是在更早的5.5版本中,innodb-large-prefix=ON之类的配置可能是无效的,纠察半天发现,原来5.5的有些版本,配置文件这个参数上设置“ON”是不认的,只能用10来处理,

知道这里就好说了,我们就换成如下设置:

innodb_file_format=Barracuda
innodb_file_per_table=1
innodb_large_prefix=1

然后重启服务器进行尝试。

如果做完以上操作,还没有完,继续报错,那很有可能我们用了utf8mb4字符集,或者索引字段确实太长了,那就只能研究这个Barracuda的东西;

我们设置的Barracuda文件格式支持4种row_format:RedundantCompactCompressedDynamic,其中Dynamic格式下,溢出的列只存储前20字节,一旦发生了行溢出,dynamic其实就存储一个指针,数据都放在溢出页里。

既然是在创建索引,我们也不能让索引数据在长度问题上吭哧吭哧死磕,这个Dynamic就给出了比较好的方案,于是,我们需要对操作表的row_format做如下设置:

alter table XXX row_format=dynamic;

或者在创建新表之初,即添加初始设置:、

create table XXX(id ...) row_format=dynamic;

这样,问题就解决了。


注意在使用过程中,有些参数设置完成之后可能没起作用,可以执行以下指令,用于确认:

SHOW ENGINES;    -- 查看引擎类型信息

SHOW VARIABLES LIKE '%innodb_file_format%';    -- 查看innodb_file_format参数信息
SHOW VARIABLES LIKE '%innodb_file_per_table%';    -- 查看innodb_file_per_table参数信息
SHOW VARIABLES LIKE '%innodb_large_prefix%';    -- 查看innodb_large_prefix参数信息

SHOW TABLE STATUS LIKE '%table_XXX%';     -- 查看表row_format参数信息

以上这些,是处理关于MySQL参数innodb_large_prefix设置的问题时总结的笔记,希望对大家有所帮助。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK