MySQL索引长度限制为767

1、当innodb_large_prefix为off,单列索引的长度限制为767。

show variables like 'innodb_large_prefix';
| innodb_large_prefix | OFF   |

字符集为utf8,单列varchar(255)是可以创建索引的上限,256即报错。

create table t2(name varchar(255));
alter table t2 add index n1(name);

create table t3(name varchar(256));
alter table t3 add index n1(name);
ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes

2、修改innodb_large_prefix为on。

set global innodb_large_prefix=1;
show variables like 'innodb_large_prefix';
| innodb_large_prefix | ON    |

3、varchar(256)是否能够建索引与表的row_format有关。

当表的row_format为Compact,单列索引的上限仍然是767。

alter table t3 add index n1(name);
ERROR 1709 (HY000): Index column size too large. The maximum column size is 767 bytes.

当表的row_format为Compressed,单列索引的上限是3072。

alter table t3 row_format=compressed key_block_size=8;
alter table t3 add index n1(name);

4、一张表的FILE_FORMAT和ROW_FORMAT

更改一张表的row_format同时也更改了这张表的file_format。Compact必定是Antelope(羚羊),dynamic和Compressed必定是Barracuda(梭鱼)。

select * from information_schema.innodb_sys_tables where name like '%t1%';
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容