mysql的子查詢的優化一直不是很友好,一直有受業界批評比較多,也是我在sql優化中遇到過最多的問題之一,你可以點擊這里 ,這里來獲得一些信息,mysql在處理子查詢的時候,會將子查詢改寫,通常情況下,我們希望由內到外,也就是先完成子查詢的結果,然后在用子查詢來驅動外查詢的表,完成查詢,但是恰恰相反,子查詢不會先被執行;今天希望通過介紹一些實際的案例來加深對mysql子查詢的理解:
案例:用戶反饋數據庫響應較慢,許多業務動更新被卡住;登錄到數據庫中觀察,發現長時間執行的sql;
| | 10437 | usr0321t9m9 | 10.242.232.50:51201 | oms | Execute | 1179 | Sending |
Sql為:
| select tradedto0_.* from a1 tradedto0_ where tradedto0_.tradestatus='1'and (tradedto0_.tradeoid in (select orderdto1_.tradeoid from a2 orderdto1_ whereorderdto1_.proname like '%??%' or orderdto1_.procode like '%??%')) and tradedto0_.undefine4='1'and tradedto0_.invoicetype='1' and tradedto0_.tradestep='0' and (tradedto0_.orderCompany like '0002%') order by tradedto0_.tradesign ASC, tradedto0_.makertime desc limit 15; |
2.其他表的更新被阻塞:
| update a1 set tradesign='DAB67634-795C-4EAC-B4A0-78F0D531D62F',markColor=' #CD5555', memotime='2012-09- 22', markPerson='??' where tradeoid in ('gy2012092204495100032') ; |
為了盡快恢復應用,將其長時間執行的sql kill掉后,應用恢復正常;
3.分析執行計劃:
| db@3306 :explain select tradedto0_.* from a1 tradedto0_ where tradedto0_.tradestatus='1' and (tradedto0_.tradeoid in (select orderdto1_.tradeoidfrom a2 orderdto1_ where orderdto1_.proname like '%??%' or orderdto1_.procode like '%??%')) and tradedto0_.undefine4='1' and tradedto0_.invoicetype='1' and tradedto0_.tradestep='0' and (tradedto0_.orderCompany like '0002%') order by tradedto0_.tradesign ASC, tradedto0_.makertime desc limit 15;+----+--------------------+------------+------+---------------+------+---------+------+-------+-----| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |+----+--------------------+------------+------+---------------+------+---------+------+-------+-----| 1 | PRIMARY | tradedto0_ | ALL | NULL | NULL | NULL | NULL | 27454 | Using where; Using filesort || 2 | DEPENDENT SUBQUERY | orderdto1_ | ALL | NULL | NULL | NULL | NULL | 40998 | Using where |+----+--------------------+------------+------+---------------+------+---------+------+-------+----- |
從執行計劃上,我們開始一步一步地進行優化:
首先,我們看看執行計劃的第二行,也就是子查詢的那部分,orderdto1_進行了全表的掃描,我們看看能不能添加適當的索引:
A.使用覆蓋索引: