MySQL 5.6 查詢最佳化工具改進

來源:互聯網
上載者:User

標籤:mysql 5.6 查詢最佳化工具改進

一.ICP

注意一下ICP的使用條件:

  1. 只能用於二級索引(secondary index)。

  2. explain顯示的執行計畫中type值(join 類型)為range、 ref、 eq_ref或者ref_or_null。且查詢需要訪問表的整行資料,即不能直接通過二級索引的元組資料獲得查詢結果(索引覆蓋)。

  3. ICP可以用於MyISAM和InnnoDB儲存引擎,不支援分區表(5.7將會解決這個問題)。

二.order by ..  limit ...最佳化

from mysql 5.6.2 開始對order by ..  limit ...最佳化:

select col1,col2 from tx order by no_key_col limit offset,rowcount;

From 5.6.2 by treating the sort buffer as a priority queue:

  • Scan the table, inserting the select list columns from each selected row in sorted order in the queue. If the queue is full, bump out the last row in the sort order.

  • Return the first N rows from the queue. (If M was specified, skip the first M rows and return the next N rows.)

Before 5.6.2,Previously, the server performed this operation by using a merge file for the sort:

  • Scan the table, repeating these steps through the end of the table:

    • Select rows until the sort buffer is filled.

    • Write the first N rows in the buffer (M+N rows if M was specified) to a merge file.

  • Sort the merge file and return the first N rows. (If M was specified, skip the first M rows and return the next N rows.)

The cost of the table scan is the same for the queue and merge-file methods, so the optimizer chooses between methods based on other costs:

  • The queue method involves more CPU for inserting rows into the queue in order

  • The merge-file method has I/O costs to write and read the file and CPU cost to sort it

演算法:

5.6使用queue:

把select 的列放入隊列裡,當隊列滿了把隊列最後的出隊列,就是把最大的、次大的、.....依次推到隊列的前面,表所有行的列全部放完後,把第一個前rowcount 出隊列,依次進行,都在sort buffer 裡排序。


5.6之前:使用 merge file

把row insert 到 sort buffer,sort buffer相當於一個堆棧,sort buffer被插滿了,依次把當前堆棧裡前面的rowcount出隊列放入到merge file,直到所有的排好序,然後通過merge file 再排序取出結果。

後續 ...........................



本文出自 “My DBA life” 部落格,請務必保留此出處http://huanghualiang.blog.51cto.com/6782683/1596176

MySQL 5.6 查詢最佳化工具改進

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.