[MySQL View]最有意思的檢視view優化過程,從30分鐘到0.08秒
開發人員寫了一個view,select要30分鐘,讓我優化下,view如下:
CREATE ALGORITHM=UNDEFINED SQL SECURITY DEFINER VIEW view_offer_label AS
SELECT ol.OFFER_ID AS OFFER_ID,ol.EFFECTIVE_DATE AS EFFECTIVE_DATE
FROM offer_label ol
WHERE(
ol.ID =
(SELECT ol2.ID
FROM offer_label ol2
WHERE ((ol.OFFER_ID = ol2.OFFER_ID) AND (ol2.LABEL = 'PROD'))
ORDER BY ol2.EFFECTIVE_DATE DESC,ol2.ID DESC LIMIT 1
)
)
開發人員select一下需要30多分鐘:
21068 rows in set (1987.08 sec)
先解析一下:
mysql> explain SELECT `ol`.`OFFER_ID` AS `OFFER_ID`,`ol`.`EFFECTIVE_DATE` AS `EFFECTIVE_DATE`
-> FROM `offer_label` `ol`
-> WHERE (`ol`.`ID` =
-> (SELECT `ol2`.`ID`
-> FROM `offer_label` `ol2`
-> WHERE ((`ol`.`OFFER_ID` = `ol2`.`OFFER_ID`) AND (`ol2`.`LABEL` = 'PROD'))
-> ORDER BY `ol2`.`EFFECTIVE_DATE` DESC,`ol2`.`ID` DESC LIMIT 1));
+----+--------------------+-------+-------+------------------------------------+-------------------+---------+---------------------------+--------+------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------------+-------+-------+------------------------------------+-------------------+---------+---------------------------+--------+------------------------------------------+
| 1 | PRIMARY | ol | index | NULL | offer_label_index | 1542 | NULL | 143299 | Using where; Using index |
| 2 | DEPENDENT SUBQUERY | ol2 | ref | OFFER_LABEL_FKEY,offer_label_index | offer_label_index | 1534 | const,catalog.ol.OFFER_ID | 1 | Using where; Using index; Using filesort |
+----+--------------------+-------+-------+------------------------------------+-------------------+---------+---------------------------+--------+------------------------------------------+
2 rows in set (0.00 sec)
看到有 Using filesort,要優化where後面的子判斷,優化如下:
select max(ol2.ID)
from offer_label ol2
where ol2.LABEL = 'PROD'
group by ol2.OFFER_ID
order by ol2.EFFECTIVE_DATE DESC,ol2.ID DESC;
mysql> explain select max(ol2.ID)
-> from offer_label ol2
-> where ol2.LABEL = 'PROD'
-> group by ol2.OFFER_ID
-> order by ol2.EFFECTIVE_DATE DESC,ol2.ID DESC;
+----+-------------+-------+------+-------------------+-------------------+---------+-------+-------+-----------------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+-------------------+-------------------+---------+-------+-------+-----------------------------------------------------------+
| 1 | SIMPLE | ol2 | ref | offer_label_index | offer_label_index | 767 | const | 71649 | Using where; Using index; Using temporary; Using filesort |
+----+-------------+-------+------+-------------------+-------------------+---------+-------+-------+-----------------------------------------------------------+
1 row in set (0.00 sec)
SELECT ol2.OFFER_ID, max(EFFECTIVE_DATE) EFFECTIVE_DATE
FROM offer_label ol2
WHERE ol2.LABEL = 'PROD'
group by ol2.OFFER_ID ;
執行結果為:
CREATE ALGORITHM=UNDEFINED SQL SECURITY DEFINER VIEW view_offer_label AS
SELECT ol.OFFER_ID AS OFFER_ID,ol.EFFECTIVE_DATE AS EFFECTIVE_DATE
FROM offer_label ol
WHERE(
ol.ID =
(SELECT ol2.ID
FROM offer_label ol2
WHERE ((ol.OFFER_ID = ol2.OFFER_ID) AND (ol2.LABEL = 'PROD'))
ORDER BY ol2.EFFECTIVE_DATE DESC,ol2.ID DESC LIMIT 1
)
)
開發人員select一下需要30多分鐘:
21068 rows in set (1987.08 sec)
先解析一下:
mysql> explain SELECT `ol`.`OFFER_ID` AS `OFFER_ID`,`ol`.`EFFECTIVE_DATE` AS `EFFECTIVE_DATE`
-> FROM `offer_label` `ol`
-> WHERE (`ol`.`ID` =
-> (SELECT `ol2`.`ID`
-> FROM `offer_label` `ol2`
-> WHERE ((`ol`.`OFFER_ID` = `ol2`.`OFFER_ID`) AND (`ol2`.`LABEL` = 'PROD'))
-> ORDER BY `ol2`.`EFFECTIVE_DATE` DESC,`ol2`.`ID` DESC LIMIT 1));
+----+--------------------+-------+-------+------------------------------------+-------------------+---------+---------------------------+--------+------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------------+-------+-------+------------------------------------+-------------------+---------+---------------------------+--------+------------------------------------------+
| 1 | PRIMARY | ol | index | NULL | offer_label_index | 1542 | NULL | 143299 | Using where; Using index |
| 2 | DEPENDENT SUBQUERY | ol2 | ref | OFFER_LABEL_FKEY,offer_label_index | offer_label_index | 1534 | const,catalog.ol.OFFER_ID | 1 | Using where; Using index; Using filesort |
+----+--------------------+-------+-------+------------------------------------+-------------------+---------+---------------------------+--------+------------------------------------------+
2 rows in set (0.00 sec)
看到有 Using filesort,要優化where後面的子判斷,優化如下:
select max(ol2.ID)
from offer_label ol2
where ol2.LABEL = 'PROD'
group by ol2.OFFER_ID
order by ol2.EFFECTIVE_DATE DESC,ol2.ID DESC;
mysql> explain select max(ol2.ID)
-> from offer_label ol2
-> where ol2.LABEL = 'PROD'
-> group by ol2.OFFER_ID
-> order by ol2.EFFECTIVE_DATE DESC,ol2.ID DESC;
+----+-------------+-------+------+-------------------+-------------------+---------+-------+-------+-----------------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+-------------------+-------------------+---------+-------+-------+-----------------------------------------------------------+
| 1 | SIMPLE | ol2 | ref | offer_label_index | offer_label_index | 767 | const | 71649 | Using where; Using index; Using temporary; Using filesort |
+----+-------------+-------+------+-------------------+-------------------+---------+-------+-------+-----------------------------------------------------------+
1 row in set (0.00 sec)
有些不對勁,再仔細看了view的結構,恍然大悟:
優化成如下樣子:
CREATE ALGORITHM=UNDEFINED SQL SECURITY DEFINER VIEW view_offer_label ASSELECT ol2.OFFER_ID, max(EFFECTIVE_DATE) EFFECTIVE_DATE
FROM offer_label ol2
WHERE ol2.LABEL = 'PROD'
group by ol2.OFFER_ID ;
執行結果為:
21068 rows in set (0.08 sec)
不到0.08秒,資料完全正確。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26230597/viewspace-1125196/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MySQL View 檢視MySqlView
- MySQL View(檢視)詳解MySqlView
- SQL優化,百萬級2張表關聯,從40分鐘到3秒的歷程SQL優化
- 深入瞭解View的事件分發過程View事件
- View 的繪製過程View
- materialized view (物化檢視)ZedView
- framework——View新增過程FrameworkView
- 探究Android View 繪製流程,Xml 檔案到 View 物件的轉換過程AndroidViewXML物件
- Android View 原始碼解析(三) – View的繪製過程AndroidView原始碼
- 檢視所有VIEW/TABLE in MSSQLViewSQL
- Android繪製View的過程研究——計算View的大小AndroidView
- hive中的 lateral view(側檢視)HiveView
- Dataguard (Standby) 相關的檢視(View)View
- Android View的繪製過程AndroidView
- 建立物化檢視MV ( Materialized View )ZedView
- 前端效能優化--從 10 多秒到 1.05 秒前端優化
- 前端效能優化–從 10 多秒到 1.05 秒前端優化
- 對資料庫檢視view的理解資料庫View
- 初探Android的View繪製過程AndroidView
- 【SQL優化器查詢變換器】檢視合併(View Merging)SQL優化View
- 從四分鐘到兩秒——談談客戶端效能優化的一些最佳實踐客戶端優化
- Android中父View和子view的點選事件的執行過程AndroidView事件
- 10分鐘理解 Android View 事件分發機制AndroidView事件
- android 動畫 ——檢視動畫(View Animation)Android動畫View
- View Transform(檢視變換)詳解ViewORM
- 檢視合併(View Merging)View
- 轉:物化檢視(Materialized View)介紹ZedView
- 在列印視窗,列印檢視View的子檢視結構圖View
- nancy Unable to find view engine that could render the view 無法找到檢視模板引擎NaNView
- Explain Extended檢視MySQL的優化AIMySql優化
- ThinkPHP框架檢視詳細介紹View檢視–模板(九)PHP框架View
- mysql view locksMySqlView
- laravel利用artisan建立view檢視檔案LaravelView
- oracle parallel並行及px檢視viewOracleParallel並行View
- android view draw原始碼過程分析AndroidView原始碼
- MV (Materialed View) 物化檢視的重新整理組View
- 淺談移動端 View 的顯示過程View
- iOS開發 如何通過檢視(view)獲取該檢視所在的控制器(viewController)iOSViewController