標籤:add index efault span 覆蓋 isa 為什麼 案例 lte
問題:
create table A (
id varchar(64) primary key,
ver int,
...
)
在id,ver上有聯合索引,10000條資料
為什麼select id from A order by id特別慢?
而select id from A order by id,ver非常快
我的表有幾個很長的欄位 varbinary(3000)
推斷: 1. 2句sql都用到了索引覆蓋,如果myisam引擎2句sql應該都很快, 推斷用的是innodb引擎2. order by id ,innodb 引擎聚簇儲存了每列的值,因為有幾個很長的欄位,1個塊存不了很多行資料,導致塊比較多,使用id主鍵時,要跨好多小檔案塊,導致效率不高。3. order by id,ver. 使用的是二級索引,innodb引擎二級索引都是存的 聚簇索引的地址指向聚簇索引,因此不帶資料,索引檔案比較小輕便,記憶體中也能使用,所以速度快。
create table t7 (
id char(64) primary key,
var int not null default 0,
str1 varchar(3000) not null,
str2 varchar(3000) not null,
str3 varchar(3000) not null,
str4 varchar(3000) not null
key `idvar` (id,var)
)engine=myisam charset=utf8;
create table t8 (
id char(64) primary key,
var int not null default 0,
str1 varchar(3000) not null,
str2 varchar(3000) not null,
str3 varchar(3000) not null,
str4 varchar(3000) not null
)engine=innodb charset=utf8;
alter table t7 add index idver(id,var)
結論: innodb 大欄位(char)主鍵 造成大量分裂, 正好發揮的是innodb的劣勢
如果沒有這麼長大欄位的列 ,差距也不會很大
alter table t8 drop column st1;alter table t8 drop column st2;alter table t8 drop column st3;
mysql經典案例分析