MySQL索引的索引長度問題

2022-07-27 03:45:10 字數 1363 閱讀 2060

**:

mysql的每個單錶中所建立的索引長度是有限制的,且對不同儲存引擎下的表有不同的限制。

在myisam表中,建立組合索引時,建立的索引長度不能超過1000,注意這裡索引的長度的計算是根據表字段設定的長度來標量的,例如:

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);

此時報錯:specified key was too long;max key length is 1000 bytes.

修改表結構:alter table test convert to charset utf8;

create index test_name3 on test(name3).

此時warning:specified key was too long;max key length is 1000 bytes.但是索引建立成功,檢視表結構可以看到建立的索引是乙個字首索引:『key test_name3(name3(333))』

得出的結論是:對於myisam表,如果建立組合索引,所建立的索引長度和不能超過1000 bytes,否則會報錯,建立失敗;對於myisam的單列索引,最大長度也不能超過1000,否則會報警,但是建立成功,最終建立的是字首索引(取前333個位元組)。

在innodb表中,建立組合索引:

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);

此時給出warning:specified key was too long;max key length is 767 bytes.

修改表結構:alter table test1 convert to charset utf8;

create index test1_name3 on test(name3).

此時給出warning:specified key was too long;max key length is 767 bytes.

得出的結論是:對於建立innodb的組合索引,如果各個列中的長度不超過767,則不再計算所有列的總長度,如果有超過767的,則給出報警,索引最後建立成功,但是對於超過767位元組的列取字首索引;對於innodb的單列索引,超過767的,給出warning,最終索引建立成功,取字首索引(取前255位元組)。

MySQL索引的索引長度問題

mysql索引的索引長度問題 specified key was too long max key length is 1000 bytes.一 修改mysql的預設儲存引擎 1 檢視mysql儲存引擎命令,在mysql 提示符下搞入show engines 字段 support為 default表...

mysql 索引長度

specified key was too long max key length is 767 bytes mysql在innodb引擎下的主鍵索引或者unique索引的最大長度為767bytes,在myisam下是1000bytes。當時我在建立unique索引的時候使用了兩個varchar 2...

mysql索引長度

大家應該知道innodb單列索引長度不能超過767bytes,聯合索引還有乙個限制是長度不能超過3072。mysql createtable tb a varchar 255 defaultnull,b varchar 255 defaultnull,c varchar 255 defaultnul...