一条经典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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • linux 防火墙 命令_Linux高频命令汇总

    linux 防火墙 命令_Linux高频命令汇总原:https://blog.csdn.net/zhang123456456/article/details/781492061、firewalld的基本使用启动:systemctlstartfirewalld查看状态:systemctlstatusfirewalld停止:systemctldisablefirewalld禁用:systemctlstop…

    2022年4月19日
  • calico文件_ico文件怎么打开

    calico文件_ico文件怎么打开k8s的网络插件,可以使用下面的,镜像可以自己修改。

    2022年10月29日
  • navicat premium mac 激活码【2021.10最新】「建议收藏」

    (navicat premium mac 激活码)好多小伙伴总是说激活码老是失效,太麻烦,关注/收藏全栈君太难教程,2021永久激活的方法等着你。IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html2JTX0APX6F-eyJsaWNlbnNlSWQi…

    2022年3月30日
  • 二叉树层序遍历Java版

    二叉树层序遍历Java版publicList<List<Integer>>levelOrder(TreeNoderoot){List<List<Integer>>result=newArrayList<>();if(root==null)returnresult;List<TreeNode>queue=newArrayList<>();queue.add(root);

    2022年5月21日
  • [紧急通告]关于蓝色理想经典论坛无法访问的声明

    [紧急通告]关于蓝色理想经典论坛无法访问的声明亲爱的蓝色理想网站及经典论坛的会员朋友们,我们在这里非常遗憾地通知大家,在未来的一段时间里,我们将不能保证经典论坛的正常访问,由于来自外部的不可抗力,论坛服务器所在的机房已对我们进行封网处理,原因是相

    2022年7月4日
  • pycharm配置tensorflow环境_python3.6对应的tensorflow版本

    pycharm配置tensorflow环境_python3.6对应的tensorflow版本Round1:打开PyCharm,点击File->Settings,点击弹出界面的“+”号,在搜索框中键入关键字“tensorflow”,从列表中选中“tensorflow”,点击下方的“InstallPackage”,等待一段时间,华丽丽的安装失败了-_-Round2:在Pycharm的terminal中运行pipinstalltensorflow,在等了半小时后,倒计时还有3个多小时,我弃疗了!安装失败!Round3:以上用的都是官方的源,现在考虑换成国

    2022年8月26日

发表回复

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

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