关于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 条件, 有时候可以提高查询效率。

相关推荐
xcLeigh9 分钟前
KES数据库性能优化实战
数据库·sql·性能优化·sql优化·数据性能
猫的玖月5 小时前
(一)MY SQL概述
数据库·sql
昇腾CANN5 小时前
TileLang-Ascend 算子性能优化方法与实操
开发语言·javascript·性能优化·昇腾·cann
ting94520006 小时前
深入解析 Social Fetch 机制:原理、架构、应用场景、实战落地与性能优化全攻略
人工智能·性能优化·架构
\xin6 小时前
pikachu自编SQL(POST)
java·数据库·sql
czlczl200209258 小时前
MySQL 性能优化:前缀索引(Prefix Index)深度解析
数据库·mysql·性能优化
HaiXCoder8 小时前
Test-Traces 分析报告
性能优化
tang&10 小时前
【MySQL】索引创建与B+树原理:MySQL性能优化的核心一课
b树·mysql·性能优化
@小柯555m12 小时前
MySql(高级操作符--高级操作符练习(1))
数据库·sql·mysql
码农阿豪12 小时前
Python 操作金仓数据库的完全指南(下篇):SQL执行、批量操作与扩展功能
数据库·python·sql