問題:
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;