MySQL延遲關(guān)聯(lián)性能優(yōu)化方法
【背景】
某業(yè)務(wù)數(shù)據(jù)庫load 報警異常,cpu usr 達到30-40 ,居高不下。使用工具查看數(shù)據(jù)庫正在執(zhí)行的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;
表的數(shù)據(jù)量大致有36w左右,該sql是一個非常典型的排序+分頁查詢:order by col limit N,OFFSET M , MySQL 執(zhí)行此類sql時需要先掃描到N行,然后再去取 M行。對于此類大數(shù)據(jù)量的排序操作,取前面少數(shù)幾行數(shù)據(jù)會很快,但是越靠后,sql的性能就會越差,因為N越大,MySQL 需要掃描不需要的數(shù)據(jù)然后在丟掉,這樣耗費大量的時間。
【分析】
針對limit 優(yōu)化有很多種方式,
1 前端加緩存,減少落到庫的查詢操作
2 優(yōu)化SQL
3 使用書簽方式 ,記錄上次查詢最新/大的id值,向后追溯 M行記錄。
4 使用Sphinx 搜索優(yōu)化。
對于第二種方式 我們推薦使用"延遲關(guān)聯(lián)"的方法來優(yōu)化排序操作,何謂"延遲關(guān)聯(lián)" :通過使用覆蓋索引查詢返回需要的主鍵,再根據(jù)主鍵關(guān)聯(lián)原表獲得需要的數(shù)據(jù)。
【解決】
根據(jù)延遲關(guān)聯(lián)的思路,修改SQL 如下:
優(yōu)化前
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)
其執(zhí)行時間:
優(yōu)化后:
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 | <derived2> | 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)
執(zhí)行時間:
優(yōu)化后 執(zhí)行時間 為原來的1/3 。
相關(guān)文章
Ubuntu 18.04下mysql 8.0 安裝配置方法圖文教程
這篇文章主要為大家詳細介紹了Ubuntu 18.04下mysql 8.0 安裝配置方法圖文教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05mysql數(shù)據(jù)庫從服務(wù)器移植到個人PC的方法
有時候本地也需要數(shù)據(jù)庫進行測試,那么就需要將服務(wù)器的東西移植到本地,如果有服務(wù)器控制權(quán)限,可以直接復制mysql的目錄(windows下),如果是別的那么就需要下面的方法了。2011-08-08sql中with?as用法以及with-as性能調(diào)優(yōu)/with用法舉例
SQL中的WITH?AS語法是一種強大的工具,可以簡化復雜查詢的編寫,提高查詢的可讀性和維護性,這篇文章主要給大家介紹了關(guān)于sql中with?as用法以及with-as性能調(diào)優(yōu)/with用法的相關(guān)資料,需要的朋友可以參考下2024-01-01