首頁  >  文章  >  資料庫  >  MySQL索引的索引長度問題具體介紹

MySQL索引的索引長度問題具體介紹

黄舟
黄舟原創
2017-03-04 15:06:472014瀏覽

  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索引的索引長度問題具體介紹的內容,更多相關內容請關注PHP中文網(www.php .cn)! #####################
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn