MySQL調優 —— Using temporary

來源:互聯網
上載者:User

標籤: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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.