欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

淺談MySQL中的子查詢優(yōu)化技巧

 更新時間:2015年05月07日 17:24:05   作者:羅龍九  
這篇文章主要介紹了淺談MySQL中的子查詢優(yōu)化技巧,子查詢的優(yōu)化是MySQL諸多優(yōu)化方法中的基本,需要的朋友可以參考下

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

案例:用戶反饋數(shù)據(jù)庫響應(yīng)較慢,許多業(yè)務(wù)動更新被卡??;登錄到數(shù)據(jù)庫中觀察,發(fā)現(xiàn)長時間執(zhí)行的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_ 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.其他表的更新被阻塞:

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

為了盡快恢復(fù)應(yīng)用,將其長時間執(zhí)行的sql kill掉后,應(yīng)用恢復(fù)正常;
3.分析執(zhí)行計劃:

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;
+----+--------------------+------------+------+---------------+------+---------+------+-------+-----
| 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 |
+----+--------------------+------------+------+---------------+------+---------+------+-------+-----

從執(zhí)行計劃上,我們開始一步一步地進行優(yōu)化:
首先,我們看看執(zhí)行計劃的第二行,也就是子查詢的那部分,orderdto1_進行了全表的掃描,我們看看能不能添加適當(dāng)?shù)乃饕?br /> A.使用覆蓋索引:

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

添加組合索引超過了最大key length限制:
B.查看該表的字段定義:

 db@3306 :DESC a2 ;
+---------------------+---------------+------+-----+---------+-------+
| FIELD        | TYPE     | NULL | KEY | DEFAULT | Extra |
+---------------------+---------------+------+-----+---------+-------+
| OID         | VARCHAR(50)  | NO  | PRI | NULL  |    |
| TRADEOID      | VARCHAR(50)  | YES |   | NULL  |    |
| PROCODE       | VARCHAR(50)  | YES |   | NULL  |    |
| PRONAME       | VARCHAR(1000) | YES |   | NULL  |    |
| SPCTNCODE      | VARCHAR(200) | YES |   | NULL  |    |

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

db@3306 :SELECT MAX(LENGTH(PRONAME)),avg(LENGTH(PRONAME)) FROM a2;
+----------------------+----------------------+
| MAX(LENGTH(PRONAME)) | avg(LENGTH(PRONAME)) |
+----------------------+----------------------+
|  95       |    24.5588 |

D.縮小字段長度

ALTER TABLE MODIFY COLUMN PRONAME VARCHAR(156);

再進行執(zhí)行計劃分析:

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;
+----+--------------------+------------+-------+-----------------+----------------------+---------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------------+------------+-------+-----------------+----------------------+---------+
| 1 | PRIMARY | tradedto0_ | ref | ind_tradestatus | ind_tradestatus | 345 | const,const,const,const | 8962 | Using where; Using filesort |
| 2 | DEPENDENT SUBQUERY | orderdto1_ | index | NULL | ind_a2 | 777 | NULL | 41005 | Using where; Using index |
+----+--------------------+------------+-------+-----------------+----------------------+---------+

發(fā)現(xiàn)性能還是上不去,關(guān)鍵在兩個表掃描的行數(shù)并沒有減?。?962*41005),上面添加的索引沒有太大的效果,現(xiàn)在查看t表的執(zhí)行結(jié)果:

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

結(jié)果集為空,所以需要將t表的結(jié)果集做作為驅(qū)動表;
4.通過上面測試驗證,普通的mysql子查詢寫法性能上是很差的,為mysql的子查詢天然的弱點,需要將sql進行改寫為關(guān)聯(lián)的寫法:

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.查看執(zhí)行計劃:

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;
+----+-------------+------------+-------+---------------+----------------------+---------+------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+-------+---------------+----------------------+---------+------+
| 1 | PRIMARY | NULL | NULL | NULL | NULL | NULL | NULL | NULL | Impossible WHERE noticed after reading const tables |
| 2 | DERIVED | orderdto1_ | index | NULL | ind_a2 | 777 | NULL | 41005 | Using where; Using index |
+----+-------------+------------+-------+---------------+----------------------+---------+------+

6.執(zhí)行時間:

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;
Empty set (0.03 sec)

縮短到了毫秒;

相關(guān)文章

  • MySQL timestamp自動更新時間分享

    MySQL timestamp自動更新時間分享

    在mysql中timestamp數(shù)據(jù)類型是一個比較特殊的數(shù)據(jù)類型,他可以自動在你不使用程序更新情況下只要你更新了記錄timestamp會自動更新時間
    2013-06-06
  • Windows下MySQL8.0.18安裝教程(圖解)

    Windows下MySQL8.0.18安裝教程(圖解)

    這篇文章主要介紹了Windows下MySQL8.0.18安裝教程,本文圖文并茂給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-11-11
  • mysql下怎樣運行腳本以運行niuzi.sql為例

    mysql下怎樣運行腳本以運行niuzi.sql為例

    mysql下運行腳本,有兩種方法,都是在命令行下進行的,需要的朋友可以記錄下
    2014-07-07
  • 教你如何在MySQL命令行中使用SQL語句的規(guī)則

    教你如何在MySQL命令行中使用SQL語句的規(guī)則

    這篇文章主要介紹了教你如何在MySQL命令行中使用SQL語句的規(guī)則 ,需要的朋友可以參考下
    2014-08-08
  • MySQL中LOW_PRIORITY含義和用法詳解

    MySQL中LOW_PRIORITY含義和用法詳解

    LOW_PRIORITY是MySQL中的一個關(guān)鍵字,它用于在執(zhí)行某些操作時改變這些操作的優(yōu)先級,本文主要介紹了MySQL中LOW_PRIORITY用法,感興趣的可以了解一下
    2024-07-07
  • MySQL多表關(guān)聯(lián)查詢方式及實際應(yīng)用

    MySQL多表關(guān)聯(lián)查詢方式及實際應(yīng)用

    MySQL語句學(xué)習(xí)的難點和重點就在于多表查詢,同時MySQL也有諸多方法供大家選擇,不論是多表聯(lián)查(聯(lián)結(jié)表、左連接、右連接……),這篇文章主要給大家介紹了關(guān)于MySQL多表關(guān)聯(lián)查詢方式及實際應(yīng)用的相關(guān)資料,需要的朋友可以參考下
    2024-07-07
  • Prometheus 監(jiān)控MySQL使用grafana展示

    Prometheus 監(jiān)控MySQL使用grafana展示

    這篇文章主要介紹prometheus通過mysql exporter+node exporter監(jiān)控mysql,并使用grafana進行圖表展示的相關(guān)內(nèi)容,感興趣的效果版可以參考下文
    2021-08-08
  • mysql中的group by高級用法

    mysql中的group by高級用法

    MySQL中的GROUP BY是數(shù)據(jù)聚合分析的核心功能,主要用于將結(jié)果集按指定列分組,并結(jié)合聚合函數(shù)進行統(tǒng)計計算,下面給大家介紹mysql中的group by用法詳解,感興趣的朋友一起看看吧
    2025-04-04
  • MySQL判斷列的值既不為NULL又不為空字符串的問題

    MySQL判斷列的值既不為NULL又不為空字符串的問題

    這篇文章主要介紹了MySQL判斷列的值既不為NULL又不為空字符串的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • MySQL中where?1=1方法的使用及改進

    MySQL中where?1=1方法的使用及改進

    這篇文章主要介紹了MySQL中where?1=1方法的使用及改進,文章主要通對where?1?=?1的使用及改進展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-05-05

最新評論