日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

浅谈mysql的子查询

發(fā)布時間:2023/12/10 数据库 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 浅谈mysql的子查询 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

2019獨角獸企業(yè)重金招聘Python工程師標準>>>

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

案例:用戶反饋數(shù)據(jù)庫響應較慢,許多業(yè)務動更新被卡住;登錄到數(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') ;
為了盡快恢復應用,將其長時間執(zhí)行的sql kill掉后,應用恢復正常;
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_進行了全表的掃描,我們看看能不能添加適當?shù)乃饕?#xff1a;
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)性能還是上不去,關鍵在兩個表掃描的行數(shù)并沒有減小(8962*41005),上面添加的索引沒有太大的效果,現(xiàn)在查看t表的執(zhí)行結果:
db@3306 :select orderdto1_.tradeoid from t orderdto1_ where orderdto1_.proname like '%??%' or orderdto1_.procode like '%??%';
Empty set (0.05 sec)
結果集為空,所以需要將t表的結果集做作為驅(qū)動表;
4.通過上面測試驗證,普通的mysql子查詢寫法性能上是很差的,為mysql的子查詢天然的弱點,需要將sql進行改寫為關聯(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)

縮短到了毫秒;

轉(zhuǎn)載于:https://my.oschina.net/sansom/blog/125752

總結

以上是生活随笔為你收集整理的浅谈mysql的子查询的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。