一条经典SQL语句优化实例

一条经典SQL语句优化实例

大家好,又见面了,我是全栈君。

 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 = ‘界’));

图片
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) ;

图片
效果明显

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

规律:
join > exists > in
union > or

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/108652.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 分类模型评估方法_政策评估的模型与方法

    分类模型评估方法_政策评估的模型与方法上图矩阵中1是正例,0是只负例对于二分类问题,可以将真实类别和分类器预测类别组合为:真正例(truepositiveTP):真实类别为正例,预测类别为正例;假正例(falsepositiveFP):真实类别为负例,预测类别为正例;假负例(falsenegativeFN):真实类别为正例,预测类别为负例;真负例(truenegativeTN):真实类别为负例,预测类别为负例;分类模…

    2022年9月27日
    1
  • Java 字符串与List 互转

    Java 字符串与List 互转1.字符串转换为ListList<String>list=Arrays.asList(str.split(“,”));2.List转换为字符串Stringstr=StringUtils.join(list,”,”);

    2022年5月15日
    44
  • MySQL查看实时执行的SQL语句

    MySQL查看实时执行的SQL语句

    2022年2月17日
    46
  • IntelliJ IDEA 2018.2 激活码(最新序列号破解)

    IntelliJ IDEA 2018.2 激活码(最新序列号破解),https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月18日
    35
  • WebService 实例应用

    WebService 实例应用两个工程分别部署在两台电脑上:webservice_client客户端  webservice_server:服务器端先说服务器导入jar包改写xml文件:cxfcom.rainspnsor.webservice.CXFNonSpringServiceImpl0cxf/services/*然后创建类:1util中:

    2022年7月21日
    14
  • js中换行_input怎么不换行

    js中换行_input怎么不换行”\n”为换行转移符,注意\n前后的空格!!!varname=$(“#name”);varname=”姓名:”+name+”\n”;2020年1月补充:一年前的文章,现在忘了当时换行是为了干什么,好像是弹出框消息太长,会自动换行。但是希望一句一句的换行。自动换行效果:\n换行效果:<!DOCTYPEhtml><…

    2025年7月8日
    5

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号