大发uu直播快3官方_大发UU快3直播平台 - 大发uu直播快3官方,大发UU快3直播平台是新浪网最重要的频道之一,24小时滚动报道国内、国际及社会新闻。每日编发新闻数以万计。

浅谈mysql的子查询

  • 时间:
  • 浏览:0

D.缩小字段长度

mysql的子查询的优化无缘无故 全部需用很友好,无缘无故 有受业界批评比较多,也是我在sql优化中遇到过最多的什么的问题之一,给你点击这里 ,这里来获得一些信息,mysql在处置子查询的事先,会将子查询改写,通常情況下,亲戚亲戚我们希望由内到外,也随后 先完成子查询的结果,随后 在用子查询来驱动外查询的表,完成查询,随后 恰恰相反,子查询太满先被执行;今天希望通过介绍一些实际的案例来加深对mysql子查询的理解:

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限制:

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

Empty set (0.05 sec)
结果集为空,什么都有需用将t表的结果集做作为驱动表;

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

再进行执行计划分析:

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 |

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

发现性能还是上不去,关键在3个 表扫描的行数并那末 减小(8962*460 5),里面再加的索引那末 太满的效果,现在查看t表的执行结果:



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)

缩短到了毫秒;

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 |

+----+--------------------+------------+------+---------------+------+---------+------+-------+-----
从执行计划上,亲戚亲戚我们刚开始一步一步地进行优化:

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

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

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

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

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

总结:

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

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

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