免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1874 | 回复: 0
打印 上一主题 下一主题

【原创】Memory & MyISAM 引擎小注意! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-11-14 15:41 |只看该作者 |倒序浏览

                今天有朋友问题,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 a123 and a2select * from t1_myisam where a1>110 and a123 and a2
语句执行计划:
explain select * from t1_memory where a1>110 and a123 and a2
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 whereexplain select * from t1_myisam where a1>110 and a123 and a2
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 a123 and a2
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
看到了吧,咱也用上了索引。哈哈。
               
               
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/29134/showart_2094613.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP