北京SEO

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.

  1. [BIGHD](root@localhost)[cm]>CREATETABLE`temp_2`(
  2. ->`id`BIGINT(20)UNSIGNEDNOTNULLAUTO_INCREMENT,
  3. ->`pn`VARCHAR(128)DEFAULT'',
  4. ->`first_path`VARCHAR(256)DEFAULT'',
  5. ->`dir`VARCHAR(255)DEFAULT'',
  6. ->`a`text,
  7. ->PRIMARYKEY(`id`)
  8. ->)ENGINE=InnoDBDEFAULTCHARSET=utf8
  9. ->
  10. ->;
  11. QueryOK,0ROWSaffected(0.15sec)
  12. [BIGHD](root@localhost)[cm]>
  13. [BIGHD](root@localhost)[cm]>
  14. [BIGHD](root@localhost)[cm]>ALTERTABLEtemp_2ADDKEY(first_path);
  15. QueryOK,0ROWSaffected,1warning(0.18sec)
  16. Records:0Duplicates:0Warnings:1
  17. [BIGHD](root@localhost)[cm]>SHOWwarnings;
  18. +---------+------+---------------------------------------------------------+
  19. |Level|Code|Message|
  20. +---------+------+---------------------------------------------------------+
  21. |Warning|1071|SpecifiedKEYwastoolong;MAXKEYLENGTHIS767bytes|
  22. +---------+------+---------------------------------------------------------+
  23. 1ROWINSET(0.00sec)
  24. [BIGHD](root@localhost)[cm]>ALTERTABLEtemp_2ADDKEY(dir);
  25. QueryOK,0ROWSaffected(0.12sec)
  26. Records:0Duplicates:0Warnings:0
  27. [BIGHD](root@localhost)[cm]>ALTERTABLEtemp_2ADDKEY(a(767));
  28. QueryOK,0ROWSaffected,1warning(0.32sec)
  29. Records:0Duplicates:0Warnings:1
  30. [BIGHD](root@localhost)[cm]>SHOWwarnings;
  31. +---------+------+---------------------------------------------------------+
  32. |Level|Code|Message|
  33. +---------+------+---------------------------------------------------------+
  34. |Warning|1071|SpecifiedKEYwastoolong;MAXKEYLENGTHIS767bytes|
  35. +---------+------+---------------------------------------------------------+
  36. 1ROWINSET(0.00sec)
  37. [BIGHD](root@localhost)[cm]>SHOWCREATETABLEtemp_2;
  38. |temp_2|CREATETABLE`temp_2`(
  39. `id`BIGINT(20)UNSIGNEDNOTNULLAUTO_INCREMENT,
  40. `pn`VARCHAR(128)DEFAULT'',
  41. `first_path`VARCHAR(256)DEFAULT'',
  42. `dir`VARCHAR(255)DEFAULT'',
  43. `a`text,
  44. PRIMARYKEY(`id`),
  45. KEY`first_path`(`first_path`(255)),
  46. KEY`dir`(`dir`),
  47. KEY`a`(`a`(255))
  48. )ENGINE=InnoDBDEFAULTCHARSET=utf8|

所以key里面a(255),表示 255个字符(一个utf8字符占3字节),再看一下联合索引的情况:

  1. [BIGHD](root@localhost)[cm]>ALTERTABLEtemp_2ADDKEY(pn,first_path);
  2. QueryOK,0ROWSaffected,1warning(0.10sec)
  3. Records:0Duplicates:0Warnings:1
  4. [BIGHD](root@localhost)[cm]>
  5. [BIGHD](root@localhost)[cm]>SHOWwarnings;
  6. +---------+------+---------------------------------------------------------+
  7. |Level|Code|Message|
  8. +---------+------+---------------------------------------------------------+
  9. |Warning|1071|SpecifiedKEYwastoolong;MAXKEYLENGTHIS767bytes|
  10. +---------+------+---------------------------------------------------------+
  11. 1ROWINSET(0.00sec)
  12. [BIGHD](root@localhost)[cm]>SHOWCREATETABLEtemp_2;
  13. |temp_2|CREATETABLE`temp_2`(
  14. `id`BIGINT(20)UNSIGNEDNOTNULLAUTO_INCREMENT,
  15. `pn`VARCHAR(128)DEFAULT'',
  16. `first_path`VARCHAR(256)DEFAULT'',
  17. `dir`VARCHAR(255)DEFAULT'',
  18. `a`text,
  19. PRIMARYKEY(`id`),
  20. KEY`first_path`(`first_path`(255)),
  21. KEY`dir`(`dir`),
  22. KEY`a`(`a`(255)),
  23. KEY`pn`(`pn`,`first_path`(255))
  24. )ENGINE=InnoDBDEFAULTCHARSET=utf8|

即使是联合索引,767的限制也是针对字段的,而不是联合索引的总长度,代码如下:

  1. CREATETABLE`temp_2`(
  2. `id`BIGINT(20)UNSIGNEDNOTNULLAUTO_INCREMENT,
  3. `a`text,
  4. `b`text,
  5. `c`text,
  6. `d`text,
  7. `e`text,
  8. PRIMARYKEY(`id`)
  9. )ENGINE=InnoDBDEFAULTCHARSET=utf8
  10. 255*3=765*4=3060+4*3=3072
  11. [BIGHD](root@localhost)[cm]>ALTERTABLEtemp_2ADDKEY(a(255),b(255),c(255),d(255),e(4));
  12. QueryOK,0ROWSaffected(0.07sec)
  13. Records:0Duplicates:0Warnings:0
  14. //phpfensi.com
  15. [BIGHD](root@localhost)[cm]>
  16. [BIGHD](root@localhost)[cm]>ALTERTABLEtemp_2ADDKEY(a(255),b(255),c(255),d(255),e(5));
  17. ERROR1071(42000):SpecifiedKEYwastoolong;MAXKEYLENGTHIS3072bytes

广告内容

mysql中innodb utf8字符集varchar索引长度问题 mysql中innodb utf8字符集varchar索引长度问题 mysql中innodb utf8字符集varchar索引长度问题

相关阅读

热门评论

SEO探索者团队 SEO探索者团队

SEO服务&网站优化

总篇数182

精选文章

RMAN中catalog和nocatalog区别介绍 小技巧:为Linux下的文件分配多个权限 zimbra8.5.1安装第三方签名ssl证书的步骤 解决mysql不能远程连接数据库方法 windows服务器mysql增量备份批处理数据库 mysql中slow query log慢日志查询分析 JavaScript跨域问题总结 Linux下负载均衡软件LVS配置(VS/DR)教程 mysql中权限参数说明 MYSQL(错误1053)无法正常启动

SEO最新算法