mysql中innodb utf8字元集varchar索引長度問題

來源:互聯網
上載者:User

索引總長度的限制是:

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]> CREATE TABLE `temp_2` (
    ->   `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
    ->   `pn` VARCHAR(128) DEFAULT '',
    ->   `first_path` VARCHAR(256) DEFAULT '',
    ->   `dir` VARCHAR(255) DEFAULT '',
    -> `a` text,
    ->   PRIMARY KEY (`id`)
    -> ) ENGINE=InnoDB DEFAULT CHARSET=utf8
    ->
    -> ;
Query OK, 0 ROWS affected (0.15 sec)
 
[BIGHD](root@localhost) [cm]>
[BIGHD](root@localhost) [cm]>
[BIGHD](root@localhost) [cm]> ALTER TABLE temp_2 ADD KEY (first_path);
Query OK, 0 ROWS affected, 1 warning (0.18 sec)
Records: 0  Duplicates: 0  Warnings: 1
 
[BIGHD](root@localhost) [cm]> SHOW warnings;
+---------+------+---------------------------------------------------------+
| Level   | Code | Message                                                 |
+---------+------+---------------------------------------------------------+
| Warning | 1071 | Specified KEY was too long; MAX KEY LENGTH IS 767 bytes |
+---------+------+---------------------------------------------------------+
1 ROW IN SET (0.00 sec)
 
[BIGHD](root@localhost) [cm]> ALTER TABLE temp_2 ADD KEY (dir);
Query OK, 0 ROWS affected (0.12 sec)
Records: 0  Duplicates: 0  Warnings: 0
 
[BIGHD](root@localhost) [cm]> ALTER TABLE temp_2 ADD KEY (a(767));
Query OK, 0 ROWS affected, 1 warning (0.32 sec)
Records: 0  Duplicates: 0  Warnings: 1
 
[BIGHD](root@localhost) [cm]> SHOW warnings;
+---------+------+---------------------------------------------------------+
| Level   | Code | Message                                                 |
+---------+------+---------------------------------------------------------+
| Warning | 1071 | Specified KEY was too long; MAX KEY LENGTH IS 767 bytes |
+---------+------+---------------------------------------------------------+
1 ROW IN SET (0.00 sec)
 
[BIGHD](root@localhost) [cm]> SHOW CREATE TABLE temp_2;
| temp_2 | CREATE TABLE `temp_2` (
  `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
  `pn` VARCHAR(128) DEFAULT '',
  `first_path` VARCHAR(256) DEFAULT '',
  `dir` VARCHAR(255) DEFAULT '',
  `a` text,
  PRIMARY KEY (`id`),
  KEY `first_path` (`first_path`(255)),
  KEY `dir` (`dir`),
  KEY `a` (`a`(255))
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
所以key裡面a(255),表示 255個字元(一個utf8字元佔3位元組)。

再看一下聯合索引的情況:

[BIGHD](root@localhost) [cm]> ALTER TABLE temp_2 ADD KEY (pn, first_path);
Query OK, 0 ROWS affected, 1 warning (0.10 sec)
Records: 0  Duplicates: 0  Warnings: 1
 
[BIGHD](root@localhost) [cm]>
[BIGHD](root@localhost) [cm]> SHOW warnings;
+---------+------+---------------------------------------------------------+
| Level   | Code | Message                                                 |
+---------+------+---------------------------------------------------------+
| Warning | 1071 | Specified KEY was too long; MAX KEY LENGTH IS 767 bytes |
+---------+------+---------------------------------------------------------+
1 ROW IN SET (0.00 sec)
 
[BIGHD](root@localhost) [cm]> SHOW CREATE TABLE temp_2;
| temp_2 | CREATE TABLE `temp_2` (
  `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
  `pn` VARCHAR(128) DEFAULT '',
  `first_path` VARCHAR(256) DEFAULT '',
  `dir` VARCHAR(255) DEFAULT '',
  `a` text,
  PRIMARY KEY (`id`),
  KEY `first_path` (`first_path`(255)),
  KEY `dir` (`dir`),
  KEY `a` (`a`(255)),
  KEY `pn` (`pn`,`first_path`(255))
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |


即使是聯合索引,767的限制也是針對欄位的,而不是聯合索引的總長度。

CREATE TABLE `temp_2` (
`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
  `a` text,
  `b` text,
  `c` text,
  `d` text,
  `e` text,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
 
 
255 * 3 = 765 * 4 = 3060  + 4*3 = 3072
[BIGHD](root@localhost) [cm]> ALTER TABLE temp_2 ADD KEY(a(255), b(255), c(255), d(255), e(4));
Query OK, 0 ROWS affected (0.07 sec)
Records: 0  Duplicates: 0  Warnings: 0
 
[BIGHD](root@localhost) [cm]>
[BIGHD](root@localhost) [cm]> ALTER TABLE temp_2 ADD KEY(a(255), b(255), c(255), d(255), e(5));
ERROR 1071 (42000): Specified KEY was too long; MAX KEY LENGTH IS 3072 bytes

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.