欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

浅谈MySQL中的子查询优化技巧

程序员文章站 2024-02-29 23:00:52
mysql的子查询的优化一直不是很友好,一直有受业界批评比较多,也是我在sql优化中遇到过最多的问题之一,你可以点击这里 ,这里来获得一些信息,mysql在处理子查询的时候...

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_ 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') ;

为了尽快恢复应用,将其长时间执行的sql kill掉后,应用恢复正常;
3.分析执行计划:

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

从执行计划上,我们开始一步一步地进行优化:
首先,我们看看执行计划的第二行,也就是子查询的那部分,orderdto1_进行了全表的扫描,我们看看能不能添加适当的索引:
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);

再进行执行计划分析:

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

发现性能还是上不去,关键在两个表扫描的行数并没有减小(8962*41005),上面添加的索引没有太大的效果,现在查看t表的执行结果:

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

结果集为空,所以需要将t表的结果集做作为驱动表;
4.通过上面测试验证,普通的mysql子查询写法性能上是很差的,为mysql的子查询天然的弱点,需要将sql进行改写为关联的写法:

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.查看执行计划:

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.执行时间:

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)

缩短到了毫秒;