首页 > 数据库技术 > 详细

mysql经典案例分析

时间:2017-02-06 13:30:30      阅读:325      评论:0      收藏:0      [点我收藏+]
问题:

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经典案例分析

原文:http://www.cnblogs.com/loveyouyou616/p/6369814.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!