mysql中innodb utf8字符集varchar索引长度问题
2019/10/10/17:36:51 阅读:1951 来源:谷歌SEO算法 标签:
Safari浏览器
在mysql中innodb为uft8字符集时发现text,varchar这些字符索引会无效了,因类innodb utf8字符集下索引的长度限制单一字段的索引长度限制为767 bytes
索引总长度的限制是:The InnoDB internal maximum key length is 3500 bytes, but MySQL itself restricts this to 3072 bytes. (1024 bytes for non-64-bit builds before MySQL 5.0.17, and for all builds before 5.0.15.)
在utf8字符集下,varchar(255) 的[数据部分]占用 255*3=765 bytes,最接近767bytes,256*3 = 768bytes,已经超过767.
- [BIGHD](root@localhost)[cm]>CREATETABLE`temp_2`(
- ->`id`BIGINT(20)UNSIGNEDNOTNULLAUTO_INCREMENT,
- ->`pn`VARCHAR(128)DEFAULT'',
- ->`first_path`VARCHAR(256)DEFAULT'',
- ->`dir`VARCHAR(255)DEFAULT'',
- ->`a`text,
- ->PRIMARYKEY(`id`)
- ->)ENGINE=InnoDBDEFAULTCHARSET=utf8
- ->
- ->;
- QueryOK,0ROWSaffected(0.15sec)
- [BIGHD](root@localhost)[cm]>
- [BIGHD](root@localhost)[cm]>
- [BIGHD](root@localhost)[cm]>ALTERTABLEtemp_2ADDKEY(first_path);
- QueryOK,0ROWSaffected,1warning(0.18sec)
- Records:0Duplicates:0Warnings:1
- [BIGHD](root@localhost)[cm]>SHOWwarnings;
- +---------+------+---------------------------------------------------------+
- |Level|Code|Message|
- +---------+------+---------------------------------------------------------+
- |Warning|1071|SpecifiedKEYwastoolong;MAXKEYLENGTHIS767bytes|
- +---------+------+---------------------------------------------------------+
- 1ROWINSET(0.00sec)
- [BIGHD](root@localhost)[cm]>ALTERTABLEtemp_2ADDKEY(dir);
- QueryOK,0ROWSaffected(0.12sec)
- Records:0Duplicates:0Warnings:0
- [BIGHD](root@localhost)[cm]>ALTERTABLEtemp_2ADDKEY(a(767));
- QueryOK,0ROWSaffected,1warning(0.32sec)
- Records:0Duplicates:0Warnings:1
- [BIGHD](root@localhost)[cm]>SHOWwarnings;
- +---------+------+---------------------------------------------------------+
- |Level|Code|Message|
- +---------+------+---------------------------------------------------------+
- |Warning|1071|SpecifiedKEYwastoolong;MAXKEYLENGTHIS767bytes|
- +---------+------+---------------------------------------------------------+
- 1ROWINSET(0.00sec)
- [BIGHD](root@localhost)[cm]>SHOWCREATETABLEtemp_2;
- |temp_2|CREATETABLE`temp_2`(
- `id`BIGINT(20)UNSIGNEDNOTNULLAUTO_INCREMENT,
- `pn`VARCHAR(128)DEFAULT'',
- `first_path`VARCHAR(256)DEFAULT'',
- `dir`VARCHAR(255)DEFAULT'',
- `a`text,
- PRIMARYKEY(`id`),
- KEY`first_path`(`first_path`(255)),
- KEY`dir`(`dir`),
- KEY`a`(`a`(255))
- )ENGINE=InnoDBDEFAULTCHARSET=utf8|
所以key里面a(255),表示 255个字符(一个utf8字符占3字节),再看一下联合索引的情况:
- [BIGHD](root@localhost)[cm]>ALTERTABLEtemp_2ADDKEY(pn,first_path);
- QueryOK,0ROWSaffected,1warning(0.10sec)
- Records:0Duplicates:0Warnings:1
- [BIGHD](root@localhost)[cm]>
- [BIGHD](root@localhost)[cm]>SHOWwarnings;
- +---------+------+---------------------------------------------------------+
- |Level|Code|Message|
- +---------+------+---------------------------------------------------------+
- |Warning|1071|SpecifiedKEYwastoolong;MAXKEYLENGTHIS767bytes|
- +---------+------+---------------------------------------------------------+
- 1ROWINSET(0.00sec)
- [BIGHD](root@localhost)[cm]>SHOWCREATETABLEtemp_2;
- |temp_2|CREATETABLE`temp_2`(
- `id`BIGINT(20)UNSIGNEDNOTNULLAUTO_INCREMENT,
- `pn`VARCHAR(128)DEFAULT'',
- `first_path`VARCHAR(256)DEFAULT'',
- `dir`VARCHAR(255)DEFAULT'',
- `a`text,
- PRIMARYKEY(`id`),
- KEY`first_path`(`first_path`(255)),
- KEY`dir`(`dir`),
- KEY`a`(`a`(255)),
- KEY`pn`(`pn`,`first_path`(255))
- )ENGINE=InnoDBDEFAULTCHARSET=utf8|
即使是联合索引,767的限制也是针对字段的,而不是联合索引的总长度,代码如下:
- CREATETABLE`temp_2`(
- `id`BIGINT(20)UNSIGNEDNOTNULLAUTO_INCREMENT,
- `a`text,
- `b`text,
- `c`text,
- `d`text,
- `e`text,
- PRIMARYKEY(`id`)
- )ENGINE=InnoDBDEFAULTCHARSET=utf8
- 255*3=765*4=3060+4*3=3072
- [BIGHD](root@localhost)[cm]>ALTERTABLEtemp_2ADDKEY(a(255),b(255),c(255),d(255),e(4));
- QueryOK,0ROWSaffected(0.07sec)
- Records:0Duplicates:0Warnings:0
- //phpfensi.com
- [BIGHD](root@localhost)[cm]>
- [BIGHD](root@localhost)[cm]>ALTERTABLEtemp_2ADDKEY(a(255),b(255),c(255),d(255),e(5));
- ERROR1071(42000):SpecifiedKEYwastoolong;MAXKEYLENGTHIS3072bytes
热门评论