码迷,mamicode.com
首页 > 数据库 > 详细

MySQL调优 —— Using temporary

时间:2015-08-04 07:05:42      阅读:281      评论:0      收藏:0      [点我收藏+]

标签:mysql   索引   优化   using temporary   order by   

  DBA发来一个线上慢查询问题, SQL如下(为突出重点省略部分内容):
select distinct article0_.id, 等字段 from article_table article0_, hits_table articlehit1_ where article0_.id=articlehit1_.id order by hits;


EXPLAIN结果:耗时4.03S
技术分享
出乎意料, 竟然会有Using temporary, order by只用到了一张表, 正常情况下不会出现借助辅助表再进行排序的情况(这种情况是多个表都涉及到排序字段才会引起的), 正常情况应该是只在排序有关系的表排序后然后就进行连接操作(例如本例的inner join)。 索引什么的其实都建好了, 看type字段就能看出来, 可参考 MySQL调优 ---- LEFT JOIN


认真一看SQL语句, 竟然在主键id上加上了distinct, 这不是白白浪费了性能, 尝试修改语句为:
select article0_.id, 等字段 from article_table article0_, hits_table articlehit1_ where article0_.id=articlehit1_.id order by hits;




EXPLAIN结果: 耗时0.04S
技术分享
竟然因为一个distinct修饰, 让MySQL认为两张表排序需要用到临时表, 真是一个奇葩的问题。 distinct非常耗时, 准备在下一篇博文详细介绍下。 

版权声明:本文为博主原创文章,未经博主允许不得转载。

MySQL调优 —— Using temporary

标签:mysql   索引   优化   using temporary   order by   

原文地址:http://blog.csdn.net/wenniuwuren/article/details/47269197

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!