关于sql 查询性能优化的小经验

最近接到一些sql优化的任务。数据库类型:DB2

有一个长sql查询效率低,大概要几十秒,大概查询逻辑如下:

select * from tableA a where exists (select 1 from tableB b where a.id=b.id )

or exists (select 1 from tableC c where a.id=c.id );

tableA ,tableB ,tableC都是大表,数据量大,最终执行时间要几十秒,

但是把or 条件改成 union all 却可以大大提升执行效率,时间降到几秒:

select * from tableA a where exists (select 1 from tableB b where a.id=b.id )

union all

select * from tableA a where exists (select 1 from tableC c where a.id=c.id );

小结:union all 替换 or 条件, 有时候可以提高查询效率。

相关推荐
vivo互联网技术1 小时前
下一代图片格式 AVIF 在 vivo 社区的落地实践
前端·性能优化·图片压缩·avif
小四的小六2 小时前
WebView 性能优化实战:从首屏1.5秒到300毫秒
性能优化·webview
lifewange2 小时前
afinfo 表设计
数据库·sql·mysql
Irene19912 小时前
SQL Developer 连接类型 (Connection Type) :SID 和 Service Name的区别
数据库·sql·连接方式
怀后同学.4 小时前
SQL注入之堆叠注入和绕过WAF
数据库·sql
Irene19915 小时前
PL/SQL:变量使用 两个连续的外部输入 注意事项
数据库·sql
顾昂_16 小时前
Web 性能优化完全指南
前端·面试·性能优化
学网安的肆伍18 小时前
【043-WEB攻防篇】PHP应用&SQL注入&符号拼接&请求方法&HTTP头&JSON&编码类
sql·安全·php
Shan120521 小时前
站在计算机领域视角看:SQL注入攻击
网络·数据库·sql