MySQL5.6最佳化了派生子查詢

來源:互聯網
上載者:User

網友黃華亮發了一條SQL的執行計畫

650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/140212/22261W124-0.jpg" title="5.5.jpg" alt="wKioL1L50MWDH3pUAAR7wzsTzM0173.jpg" />

問t表的欄位TOP_TITLE_ID是主鍵,為啥order by還會有Using temporary; Using filesort


後來我把該表要了過來,在我的機器上做了下測試,如果改成如下SQL就沒有Using temporary; Using filesort

650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/140212/22261Q151-1.jpg" title="5.5_1.jpg" alt="wKiom1L50h-iYgV1AAN1s01J8DI458.jpg" />


於是又在5.6上測試了一下,發現:

650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/140212/22261S146-2.jpg" title="5.6.jpg" alt="wKiom1L50oThf_doAAUcH1PMmyc418.jpg" />

同樣的SQL語句,在MySQL5.6上就去掉了Using temporary; Using filesort


仔細查看了手冊,

650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/140212/22261RW5-3.jpg" style="float:none;" title="5.6_1.jpg" alt="wKiom1L504PysREXAAD0IMIxVzs550.jpg" />

650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/140212/22261Q218-4.jpg" style="float:none;" title="5.6_2.jpg" alt="wKioL1L501_jtwOZAAFHPAggZSQ654.jpg" />

就是說採用一個索引將子查詢的結果存進一個暫存資料表,並且利用這個暫存資料表來執行一個串連。這裡c表TOP_TITLE_ID欄位是索引,關聯t表的欄位TOP_TITLE_ID是主鍵,Using temporary; Using filesort也就消失了。



本文出自 “賀春暘的技術專欄” 部落格,請務必保留此出處http://hcymysql.blog.51cto.com/5223301/1358049

相關文章

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.