国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > MySQL > 正文

淺談MySQL中的子查詢優化技巧

2024-07-24 13:07:07
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了淺談MySQL中的子查詢優化技巧,子查詢的優化是MySQL諸多優化方法中的基本,需要的朋友可以參考下

mysql的子查詢的優化一直不是很友好,一直有受業界批評比較多,也是我在sql優化中遇到過最多的問題之一,你可以點擊這里 ,這里來獲得一些信息,mysql在處理子查詢的時候,會將子查詢改寫,通常情況下,我們希望由內到外,也就是先完成子查詢的結果,然后在用子查詢來驅動外查詢的表,完成查詢,但是恰恰相反,子查詢不會先被執行;今天希望通過介紹一些實際的案例來加深對mysql子查詢的理解:

案例:用戶反饋數據庫響應較慢,許多業務動更新被卡住;登錄到數據庫中觀察,發現長時間執行的sql;

 

 
  1. | 10437 | usr0321t9m9 | 10.242.232.50:51201 | oms | Execute | 1179 | Sending 

Sql為:

 

 
  1. select tradedto0_.* from a1 tradedto0_ where tradedto0_.tradestatus='1' 
  2. and (tradedto0_.tradeoid in (select orderdto1_.tradeoid from a2 orderdto1_ where 
  3. orderdto1_.proname like '%??%' or orderdto1_.procode like '%??%')) and tradedto0_.undefine4='1' 
  4. and tradedto0_.invoicetype='1' and tradedto0_.tradestep='0' and (tradedto0_.orderCompany like '0002%') order by tradedto0_.tradesign ASC, tradedto0_.makertime desc limit 15; 

2.其他表的更新被阻塞:

 

 
  1. update a1 set tradesign='DAB67634-795C-4EAC-B4A0-78F0D531D62F'
  2. markColor=' #CD5555', memotime='2012-09- 22', markPerson='??' where tradeoid in ('gy2012092204495100032') ; 

為了盡快恢復應用,將其長時間執行的sql kill掉后,應用恢復正常;

3.分析執行計劃:

 

 
  1. db@3306 :explain select tradedto0_.* from a1 tradedto0_ where tradedto0_.tradestatus='1' and (tradedto0_.tradeoid in (select orderdto1_.tradeoid 
  2. from 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; 
  3. +----+--------------------+------------+------+---------------+------+---------+------+-------+----- 
  4. | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | 
  5. +----+--------------------+------------+------+---------------+------+---------+------+-------+----- 
  6. | 1 | PRIMARY | tradedto0_ | ALL | NULL | NULL | NULL | NULL | 27454 | Using where; Using filesort | 
  7. | 2 | DEPENDENT SUBQUERY | orderdto1_ | ALL | NULL | NULL | NULL | NULL | 40998 | Using where | 
  8. +----+--------------------+------------+------+---------------+------+---------+------+-------+----- 

從執行計劃上,我們開始一步一步地進行優化:

首先,我們看看執行計劃的第二行,也就是子查詢的那部分,orderdto1_進行了全表的掃描,我們看看能不能添加適當的索引:

A.使用覆蓋索引:

 

 
  1. db@3306:alter table a2 add index ind_a2(proname,procode,tradeoid); 
  2. ERROR 1071 (42000): Specified key was too long; max key length is 1000 bytes 

添加組合索引超過了最大key length限制:

B.查看該表的字段定義:

 

 
  1. db@3306 :DESC a2 ; 
  2. +---------------------+---------------+------+-----+---------+-------+ 
  3. | FIELD | TYPE | NULL | KEY | DEFAULT | Extra | 
  4. +---------------------+---------------+------+-----+---------+-------+ 
  5. | OID | VARCHAR(50) | NO | PRI | NULL | | 
  6. | TRADEOID | VARCHAR(50) | YES | | NULL | | 
  7. | PROCODE | VARCHAR(50) | YES | | NULL | | 
  8. | PRONAME | VARCHAR(1000) | YES | | NULL | | 
  9. | SPCTNCODE | VARCHAR(200) | YES | | NULL | | 

C.查看表字段的平均長度:

 

 
  1. db@3306 :SELECT MAX(LENGTH(PRONAME)),avg(LENGTH(PRONAME)) FROM a2; 
  2. +----------------------+----------------------+ 
  3. | MAX(LENGTH(PRONAME)) | avg(LENGTH(PRONAME)) | 
  4. +----------------------+----------------------+ 
  5. | 95 | 24.5588 | 

D.縮小字段長度

 

 
  1. ALTER TABLE MODIFY COLUMN PRONAME VARCHAR(156); 

再進行執行計劃分析:

 

 
  1. db@3306 :explain select tradedto0_.* from a1 tradedto0_ where tradedto0_.tradestatus='1' and (tradedto0_.tradeoid in (select orderdto1_.tradeoid from 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; 
  2. +----+--------------------+------------+-------+-----------------+----------------------+---------+ 
  3. | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | 
  4. +----+--------------------+------------+-------+-----------------+----------------------+---------+ 
  5. | 1 | PRIMARY | tradedto0_ | ref | ind_tradestatus | ind_tradestatus | 345 | const,const,const,const | 8962 | Using where; Using filesort | 
  6. | 2 | DEPENDENT SUBQUERY | orderdto1_ | index | NULL | ind_a2 | 777 | NULL | 41005 | Using where; Using index | 
  7. +----+--------------------+------------+-------+-----------------+----------------------+---------+ 

發現性能還是上不去,關鍵在兩個表掃描的行數并沒有減小(8962*41005),上面添加的索引沒有太大的效果,現在查看t表的執行結果:

 

 
  1. db@3306 :select orderdto1_.tradeoid from t orderdto1_ where orderdto1_.proname like '%??%' or orderdto1_.procode like '%??%'
  2. Empty set (0.05 sec) 

結果集為空,所以需要將t表的結果集做作為驅動表;

4.通過上面測試驗證,普通的mysql子查詢寫法性能上是很差的,為mysql的子查詢天然的弱點,需要將sql進行改寫為關聯的寫法:

 

 
  1. select tradedto0_.* from a1 tradedto0_ ,(select orderdto1_.tradeoid from a2 orderdto1_ where orderdto1_.proname like '%??%' or orderdto1_.procode like '%??%')t2 where tradedto0_.tradestatus='1' and (tradedto0_.tradeoid=t2.tradeoid ) 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; 

5.查看執行計劃:

 

 
  1. db@3306 :explain select tradedto0_.* from a1 tradedto0_ ,(select orderdto1_.tradeoid from a2 orderdto1_ where orderdto1_.proname like '%??%' or orderdto1_.procode like '%??%')t2 where tradedto0_.tradestatus='1' and (tradedto0_.tradeoid=t2.tradeoid ) 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. +----+-------------+------------+-------+---------------+----------------------+---------+------+ 
  3. | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | 
  4. +----+-------------+------------+-------+---------------+----------------------+---------+------+ 
  5. | 1 | PRIMARY | NULL | NULL | NULL | NULL | NULL | NULL | NULL | Impossible WHERE noticed after reading const tables | 
  6. | 2 | DERIVED | orderdto1_ | index | NULL | ind_a2 | 777 | NULL | 41005 | Using where; Using index | 
  7. +----+-------------+------------+-------+---------------+----------------------+---------+------+ 

6.執行時間:

 

 
  1. db@3306 :select tradedto0_.* from a1 tradedto0_ ,(select orderdto1_.tradeoid from a2 orderdto1_ where orderdto1_.proname like '%??%' or orderdto1_.procode like '%??%')t2 where tradedto0_.tradestatus='1' and (tradedto0_.tradeoid=t2.tradeoid ) 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. Empty set (0.03 sec) 

縮短到了毫秒;

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 行唐县| 乡宁县| 龙游县| 德庆县| 鄱阳县| 周至县| 宁晋县| 红桥区| 新建县| 和平县| 滕州市| 商都县| 泽普县| 八宿县| 乌拉特前旗| 普定县| 寿光市| 治多县| 闽清县| 建始县| 射阳县| 柳江县| 康乐县| 五常市| 永定县| 永兴县| 华坪县| 鄯善县| 凤凰县| 宜春市| 故城县| 深水埗区| 民勤县| 时尚| 循化| 郯城县| 清水县| 绥德县| 灵丘县| 镇安县| 蓬溪县|