博客统计信息

51cto博客之星
用户名:david_yeung
文章数:205
评论数:178
访问量:282972
无忧币:2857
博客积分:3922
博客等级:7
注册日期:2008-06-05

Memory & MyISAM 引擎小注意!
2009-11-18 09:29:31
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://yueliangdao0608.blog.51cto.com/397025/228925
今天有朋友问题,MEMORY 引擎的表查询速度竟然比MYISAM引擎慢!
熟读手册后,你就不用有这样的疑问了。

我们来小解决下。
示例表结构:
create table t1_memory (
id int unsigned not null auto_increment primary key,
a1 decimal(15,12),
a2 decimal(15,12),
remark varchar(200) not null,
key idx_u1 (a1,a2)
) engine memory;

create table t1_myisam (
id int unsigned not null auto_increment primary key,
a1 decimal(15,12),
a2 decimal(15,12),
remark varchar(200) not null,
key idx_u1 (a1,a2)
) engine myisam;
示例SQL语句:
select * from t1_memory where a1>110 and a1<111 and a2>23 and a2<24;
select * from t1_myisam where a1>110 and a1<111 and a2>23 and a2<24;

语句执行计划:
explain
select * from t1_memory where a1>110 and a1<111 and a2>23 and a2<24;

query result

id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1_memory ALL idx_u1 (NULL) (NULL) (NULL) 3000 Using where

explain
select * from t1_myisam where a1>110 and a1<111 and a2>23 and a2<24;

query result

id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1_myisam range idx_u1 idx_u1 9 (NULL) 1 Using where

根本原因就是默认MEMORY 引擎采用HASH索引, 所以对于RANGE INDEX 来说,我们要修改成BTREE索引。
解决办法:
变化索引类型
alter table t1_memory drop key idx_u1, add key idx_u1 using btree (a1,a2);

优化后执行计划:
explain
select * from t1_memory where a1>110 and a1<111 and a2>23 and a2<24;

query result

id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1_memory range idx_u1 idx_u1 9 (NULL) 2 Using where

看到了吧,咱也用上了索引。哈哈。

本文出自 “上帝,咱们不见不散!” 博客,请务必保留此出处http://yueliangdao0608.blog.51cto.com/397025/228925

分享至
更多
一键收藏,随时查看,分享好友!
0人
了这篇文章
类别:MySQL性能优化技术圈()┆阅读()┆评论() ┆ 推送到技术圈返回首页

文章评论

 
2009-11-18 09:39:08
哈,这回没问题了~

 

发表评论            

【技术门诊】专家解析:软考重点难点及应试技巧
昵  称:
登录  快速注册
验证码:

请点击后输入验证码博客过2级,无需填写验证码

内  容: