MySQL索引的索引长度问题
轉(zhuǎn)自:http://samyubw.blog.51cto.com/978243/223773
MySQL的每個(gè)單表中所創(chuàng)建的索引長(zhǎng)度是有限制的,且對(duì)不同存儲(chǔ)引擎下的表有不同的限制。
在MyISAM表中,創(chuàng)建組合索引時(shí),創(chuàng)建的索引長(zhǎng)度不能超過(guò)1000,注意這里索引的長(zhǎng)度的計(jì)算是根據(jù)表字段設(shè)定的長(zhǎng)度來(lái)標(biāo)量的,例如:
create table test(id int,name1 varchar(300),name2 varchar(300),name3 varchar(500))charset=latin1 engine=myisam;
create index test_name on test(name1,name2,name3);
此時(shí)報(bào)錯(cuò):Specified key was too long;max key length is 1000 bytes.
修改表結(jié)構(gòu):alter table test convert to charset utf8;
create index test_name3 on test(name3).
此時(shí)warning:Specified key was too long;max key length is 1000 bytes.但是索引創(chuàng)建成功,查看表結(jié)構(gòu)可以看到創(chuàng)建的索引是一個(gè)前綴索引:‘key test_name3(name3(333))’
得出的結(jié)論是:對(duì)于myisam表,如果創(chuàng)建組合索引,所創(chuàng)建的索引長(zhǎng)度和不能超過(guò)1000 bytes,否則會(huì)報(bào)錯(cuò),創(chuàng)建失敗;對(duì)于myisam的單列索引,最大長(zhǎng)度也不能超過(guò)1000,否則會(huì)報(bào)警,但是創(chuàng)建成功,最終創(chuàng)建的是前綴索引(取前333個(gè)字節(jié))。
在Innodb表中,創(chuàng)建組合索引:
create table test1(id int,name1 varchar(300),name2 varchar(300),name3 varchar(500))charset=latin1 engine=innodb;
create index test1_name on test(name1,name2,name3);
此時(shí)給出warning:Specified key was too long;max key length is 767 bytes.
修改表結(jié)構(gòu):alter table test1 convert to charset utf8;
create index test1_name3 on test(name3).
此時(shí)給出warning:Specified key was too long;max key length is 767 bytes.
得出的結(jié)論是:對(duì)于創(chuàng)建innodb的組合索引,如果各個(gè)列中的長(zhǎng)度不超過(guò)767,則不再計(jì)算所有列的總長(zhǎng)度,如果有超過(guò)767的,則給出報(bào)警,索引最后創(chuàng)建成功,但是對(duì)于超過(guò)767字節(jié)的列取前綴索引;對(duì)于innodb的單列索引,超過(guò)767的,給出warning,最終索引創(chuàng)建成功,取前綴索引(取前255字節(jié))。
轉(zhuǎn)載于:https://www.cnblogs.com/Michaelwjw/p/6373892.html
總結(jié)
以上是生活随笔為你收集整理的MySQL索引的索引长度问题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Swift UISearchContro
- 下一篇: Django ORM 数据库操作