这个SQL如何优化

xiongbing528 2012-09-21 11:00:04
explain select d.*,ds.* from (select did as did ,sum(down_count) as c from mcuhome_downs_salt group by did order by c desc) ds , mcuhome_downs d where ds.did = d.did order by ds.c desc limit 10


1 PRIMARY <derived2> ALL 2
1 PRIMARY d eq_ref PRIMARY PRIMARY 3 ds.did 1
2 DERIVED mcuhome_downs_salt ALL 2 Using temporary; Using filesort

如果我增加以下索引
create index count_did on mcuhome_downs_salt(did ,down_count)

explain select d.*,ds.* from (select did as did ,sum(down_count) as c from mcuhome_downs_salt group by did order by c desc) ds , mcuhome_downs d where ds.did = d.did order by ds.c desc limit 10


结果是这个
1 PRIMARY <derived2> ALL 2
1 PRIMARY d eq_ref PRIMARY PRIMARY 3 ds.did 1
2 DERIVED mcuhome_downs_salt index count_did 6 2 Using index; Using temporary; Using filesort


变成了走 index索引了

show index from mcuhome_downs_salt

mcuhome_downs_salt 0 PRIMARY 1 dsid A 2 BTREE
mcuhome_downs_salt 1 count_did 1 did A BTREE
mcuhome_downs_salt 1 count_did 2 down_count A BTREE


但是还是有部分检索是走全表扫描,请问这个SQL还有优化的余地吗。
...全文
99 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
rucypli 2012-09-21
  • 打赏
  • 举报
回复
select did as did ,sum(down_count) as c from mcuhome_downs_salt group by did order by c desc
加快这个是关键
xiongbing528 2012-09-21
  • 打赏
  • 举报
回复
这条SQL有优化的余地吗

56,875

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧