MySQL索引长度限制为767

2019-10-28  本文已影响0人  李得志_春风得意马蹄疾

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%';
上一篇 下一篇

猜你喜欢

热点阅读