亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

Memory & MyISAM 引擎小注意!

標簽:
MySQL


今天有朋友问题,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

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

©著作权归作者所有:来自51CTO博客作者david_yeung的原创作品,如需转载,请注明出处,否则将追究法律责任

MySQLMEMORYMySQL性能优化


點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消