MySQL社区

 找回密码
 注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

搜索
查看: 16284|回复: 14
打印 上一主题 下一主题

[SQL类] 一条经典的SQL语句优化实例

[复制链接]
跳转到指定楼层
1#
发表于 2007-9-3 15:38:58 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
1、概述
如下SQL语句发生严重消耗资源的问题,使得OS's load average会在30以上,一条语句需要执行上百秒。

/*
PIXPatient 184176条
DomainPatient 184189条
PersonName 184189条
*/

捕获的SQL语句:
select * from PIXPatient where PIXPatientTID
in (select distinct PIXPatientTID from DomainPatient where DomainPatientTID
in ( select DomainPatientTID from DomainPatient
  where PatientBirthday = '1994-01-09' or PatientBirthday = '1994-01-01'
  union select  DomainPatientTID from PersonName where FamilyName = '倪' or GivenName = '界'));

2、优化

a.优化前执行效率:
mysql> select * from PIXPatient where PIXPatientTID
in (select distinct PIXPatientTID from DomainPatient where DomainPatientTID
in ( select DomainPatientTID from DomainPatient
  where PatientBirthday = '1994-01-09' or PatientBirthday = '1994-01-01'
  union select  DomainPatientTID from PersonName where FamilyName = '倪' or GivenName = '界'));

+---------------+--------------+------------------+
| PIXPatientTID | PIXPatientID | PIXPatientStatus |
+---------------+--------------+------------------+
|         17013 | 17013        | active           |
|         35491 | 35491        | active           |
|         39654 | 39654        | active           |
|         47351 | 47351        | active           |
|         77820 | 77820        | active           |
|        111464 | 111464       | active           |
|        151700 | 151700       | active           |
|        168993 | 168993       | active           |
+---------------+--------------+------------------+
8 rows in set (1 min 58.11 sec)

8 rows in set (23.80 sec) ---- 空闲时

b.加索引
alter table PersonName add index Index_FamilyName (FamilyName), add index Index_GivenName (GivenName);
alter table DomainPatient add index Index_PatientBirthday (PatientBirthday);

效果不明显

c.重构SQL语句(优化)
mysql> select * from PIXPatient inner join (
    -> select distinct PIXPatientTID from DomainPatient inner join (
    -> select DomainPatientTID from DomainPatient  where PatientBirthday = '1994-01-09'
    -> union select DomainPatientTID from DomainPatient  where PatientBirthday = '1994-01-01'
    -> union select  DomainPatientTID from PersonName where FamilyName = '倪'
    -> union select  DomainPatientTID from PersonName where GivenName = '界' ) a using(DomainPatientTID) ) b using(PIXPatientTID) ;
+---------------+--------------+------------------+
| PIXPatientTID | PIXPatientID | PIXPatientStatus |
+---------------+--------------+------------------+
|         77820 | 77820        | active           |
|        168993 | 168993       | active           |
|         17013 | 17013        | active           |
|         35491 | 35491        | active           |
|         39654 | 39654        | active           |
|         47351 | 47351        | active           |
|        111464 | 111464       | active           |
|        151700 | 151700       | active           |
+---------------+--------------+------------------+
8 rows in set
(1.11 sec)

8 rows in set (0.02 sec) ---- 空闲时

效果明显

3、结论

SQL语句中,尽量避免使用or,in关键字,因为执行效率低。


规律:

join > exists > in

union > or




分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
收藏收藏1 分享淘帖 顶 踩
推荐
发表于 2013-8-20 11:14:56 | 只看该作者
我最近也在做优化。
不过和你做的有点不一样。
Version : 5.0.96
SELECT count(*) from Table1 inner join table2 on table1.id = table2.id
table1 有1000w  table2有1000。两个表均没有建立索引。
耗时10分钟。 程序全线卡死。

优化后。
SELECT count(*) from Table1 where id in (select id from  table2 )
耗时6秒。

所以我的结论是in未必比Inner慢 。需要看场合。
beingman 该用户已被删除
推荐
发表于 2012-6-18 14:39:58 | 只看该作者
提示: 作者被禁止或删除 内容自动屏蔽
4#
发表于 2008-4-9 19:29:08 | 只看该作者
高啊
5#
发表于 2010-5-28 10:47:08 | 只看该作者
学习了,谢谢!
6#
发表于 2011-11-24 15:19:54 | 只看该作者
学习了:一条经典的SQL语句优化实例
yeekum 该用户已被删除
7#
发表于 2013-8-17 19:57:49 | 只看该作者
提示: 作者被禁止或删除 内容自动屏蔽
8#
发表于 2013-11-12 14:54:21 | 只看该作者
学习了无缝钢管
11#
发表于 2015-2-7 14:37:11 | 只看该作者

嗯,不错,对于新手很有帮助
12#
发表于 2015-2-7 14:42:09 | 只看该作者

嗯,不错,对于新手很有帮助
13#
发表于 2015-4-17 10:18:03 | 只看该作者
嗯,不错,对于新手很有帮助.8楼说的也有道理
15#
发表于 2015-7-10 10:01:05 | 只看该作者
我觉得所有的优化最终都是优化IO~
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|申请友链|小黑屋|Archiver|手机版|MySQL社区 ( 京ICP备07012489号   
联系人:周生; 联系电话:13911732319

GMT+8, 2024-4-25 19:46 , Processed in 0.073105 second(s), 26 queries , Gzip On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表