zoukankan      html  css  js  c++  java
  • MySQL调优 —— Using temporary

      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很耗时, 准备在下一篇博文具体介绍下。 

  • 相关阅读:
    二叉树的遍历
    98验证二叉搜索树
    104二叉树的最大深度
    101对称二叉树
    100相同的树
    递归算法
    52N皇后II
    51N皇后
    90子集II
    526优美的排列
  • 原文地址:https://www.cnblogs.com/jzssuanfa/p/6894964.html
Copyright © 2011-2022 走看看