浅谈mysql的子查询

  • 时间:
  • 浏览:0
  • 来源:万人炸金花IOS_万人炸金花APP下载

再进行执行计划分析:

db@360 6 :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 | 460 5 | Using where; Using index |

+----+--------------------+------------+-------+-----------------+----------------------+---------+

发现性能还是上不去,关键在有四个 表扫描的行数并越来越减小(8962*460 5),里边换成的索引越来越太大的效果,现在查看t表的执行结果:

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

| 10437 | usr0321t9m9 | 10.242.232.60 :51201 | oms | Execute | 1179 | Sending

db@360 6:alter table a2 add index ind_a2(proname,procode,tradeoid);

ERROR 1071 (460 0): Specified key was too long; max key length is 60 0 bytes
换成组合索引超过了最大key length限制:

mysql的子查询的优化一直 都是 很友好,一直 有受业界批评比较多,也是我在sql优化中遇到过最多的难题图片之一,让人点击这里 ,这里来获得许多信息,mysql在正确处理子查询的很久,会将子查询改写,通常具体情况下,大伙希望由内到外,也可是先完成子查询的结果,怎么让在用子查询来驱动外查询的表,完成查询,怎么让恰恰相反,子查询无需先被执行;今天希望通过介绍许多实际的案例来加深对mysql子查询的理解:



db@360 6 :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)

缩短到了毫秒;

D.缩小字段长度

总结:

1. mysql子查询在执行计划上有着明显的弱点,需要将子查询进行改写

可需要参考:

a. 生产库中遇到mysql的子查询:http://hidba.org/?p=412

b. 内建的builtin InnoDB,子查询阻塞更新:http://hidba.org/?p=456

2. 在表底部形态设计上,暂且随便使用varchar(N)的大字段,导致 着无法使用索引

可需要参考:

a. JDBC内存管理—varchar2(60 0)的影响:http://hidba.org/?p=31

b. innodb中大字段的限制:http://hidba.org/?p=144

c. innodb使用大字段text,blob的许多优化建议: http://hidba.org/?p=551

db@360 6 :select orderdto1_.tradeoid from t orderdto1_ where orderdto1_.proname like '%??%' or orderdto1_.procode like '%??%';

Empty set (0.05 sec)
结果集为空,许多需要将t表的结果集做作为驱动表;

案例:用户反馈数据库响应较慢,许多业务动更新被卡住;登录到数据库中观察,发现长时间执行的sql;

db@360 6 :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 |

+----+--------------------+------------+------+---------------+------+---------+------+-------+-----
从执行计划上,大伙现在现在开始 英语 一步一步地进行优化:

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

update a1 set tradesign='DAB67634-795C-4EAC-B4A0-78F0D531D62F',

markColor=' #CD5555', memotime='2012-09- 22', markPerson='??' where tradeoid in ('gy201209220449560 032') ;
为了尽快恢复应用,将其长时间执行的sql kill掉后,应用恢复正常;

db@360 6 :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 | 460 5 | Using where; Using index |

+----+-------------+------------+-------+---------------+----------------------+---------+------+

6.执行时间:

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.许多表的更新被阻塞: