1.mysql執行計劃中會有一列 Extra mysql
Using filesort 是Mysql裏一種速度比較慢的外部排序,咱們能夠經過優化索引來儘可能避免出現Using filesort,從而提升速度。通常是order by 後面的字段沒有加index 致使sql
Using temporary 說明用到了臨時表,通常是用了group by的緣由,建立一個臨時表用以執行分組操做優化
Using temporary, Using filesort 是比較糟糕的,要儘可能排除排序
2.優化GROUP BY語句索引
默認狀況下,MySQL對全部GROUP BY col1,col2...的字段進行排序,若是查詢包括GROUP BY 但用戶想要避免排序結果的消耗,則能夠指定ORDER By NULL禁止排序:file
explain select id, sum(moneys) from sales2 group by id \G explain select id, sum(moneys) from sales2 group by id order by null \G select