MySQL延遲關聯效能最佳化方法

來源:互聯網
上載者:User

   這篇文章主要介紹了MySQL延遲關聯效能最佳化方法,本文講解了延遲關聯的背景、延遲關聯的分析、延遲關聯的解決等內容,需要的朋友可以參考下

  【背景】

  某業務資料庫load 警示異常,cpu usr 達到30-40 ,居高不下。使用工具查看資料庫正在執行的sql ,排在前面的大部分是:

   代碼如下:

  SELECT id, cu_id, name, info, biz_type, gmt_create, gmt_modified,start_time, end_time, market_type, back_leaf_category,item_status,picuture_url FROM relation where biz_type ='0' AND end_time >='2014-05-29' ORDER BY id asc LIMIT 149420 ,20;

  表的資料量大致有36w左右,該sql是一個非常典型的排序+分頁查詢:order by col limit N,OFFSET M , MySQL 執行此類sql時需要先掃描到N行,然後再去取 M行。對於此類大資料量的排序操作,取前面少數幾行資料會很快,但是越靠後,sql的效能就會越差,因為N越大,MySQL 需要掃描不需要的資料然後在丟掉,這樣耗費大量的時間。

  【分析】

  針對limit 最佳化有很多種方式,

  1 前端加緩衝,減少落到庫的查詢操作

  2 最佳化SQL

  3 使用書籤方式 ,記錄上次查詢最新/大的id值,向後追溯 M行記錄。

  4 使用Sphinx 搜尋最佳化。

  對於第二種方式 我們推薦使用"延遲關聯"的方法來最佳化排序操作,何謂"延遲關聯" :通過使用覆蓋索引查詢返回需要的主鍵,再根據主鍵關聯原表獲得需要的資料。

  【解決】

  根據延遲關聯的思路,修改SQL 如下:

  最佳化前

   代碼如下:

  root@xxx 12:33:48>explain SELECT id, cu_id, name, info, biz_type, gmt_create, gmt_modified,start_time, end_time, market_type, back_leaf_category,item_status,picuture_url FROM relation where biz_type ='0' AND end_time >='2014-05-29' ORDER BY id asc LIMIT 149420 ,20;

  +----+-------------+-------------+-------+---------------+-------------+---------+------+--------+-----------------------------+

  | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |

  +----+-------------+-------------+-------+---------------+-------------+---------+------+--------+-----------------------------+

  | 1 | SIMPLE | relation | range | ind_endtime | ind_endtime | 9 | NULL | 349622 | Using where; Using filesort |

  +----+-------------+-------------+-------+---------------+-------------+---------+------+--------+-----------------------------+

  1 row in set (0.00 sec)

  其執行時間:

  最佳化後:

  代碼如下:

  SELECT a.* FROM relation a, (select id from relation where biz_type ='0' AND end_time >='2014-05-29' ORDER BY id asc LIMIT 149420 ,20 ) b where a.id=b.id

  代碼如下:

  root@xxx 12:33:43>explain SELECT a.* FROM relation a, (select id from relation where biz_type ='0' AND end_time >='2014-05-29' ORDER BY id asc LIMIT 149420 ,20 ) b where a.id=b.id;

  +----+-------------+-------------+--------+---------------+---------+---------+------+--------+-------+

  | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |

  +----+-------------+-------------+--------+---------------+---------+---------+------+--------+-------+

  | 1 | PRIMARY | | ALL | NULL | NULL | NULL | NULL | 20 | |

  | 1 | PRIMARY | a | eq_ref | PRIMARY | PRIMARY | 8 | b.id | 1 | |

  | 2 | DERIVED | relation | index | ind_endtime | PRIMARY | 8 | NULL | 733552 | |

  +----+-------------+-------------+--------+---------------+---------+---------+------+--------+-------+

  3 rows in set (0.36 sec)

  執行時間:

  最佳化後 執行時間 為原來的1/3 。

聯繫我們

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