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

相关推荐
桦说编程2 小时前
如果让我从头再来学习并发编程
java·设计模式·性能优化
chxii3 小时前
MyBatis 动态 SQL,通过 XML (如 <if>、<foreach> 等)实现灵活的 SQL 拼接。
xml·sql·mybatis
合作小小程序员小小店8 小时前
桌面开发,在线%幼儿教育考试管理%系统,基于eclipse,java,swing,mysql数据库
java·数据库·sql·mysql·eclipse·jdk
n***293210 小时前
前端动画性能优化,减少重绘重排
前端·性能优化
朝新_10 小时前
Spring事务和事务传播机制
数据库·后端·sql·spring·javaee
dblens 数据库管理和开发工具10 小时前
PostgreSQL物化视图详解:用空间换时间的性能优化利器
数据库·postgresql·性能优化
MichaelIp11 小时前
Python同步vs异步性能对比实验-2
开发语言·python·性能优化·可用性测试
奔跑吧邓邓子12 小时前
Server性能优化实战:突破性能瓶颈,释放强大算力
性能优化·实战·server·突破性能瓶颈
百***349513 小时前
Python连接SQL SEVER数据库全流程
数据库·python·sql
l1t1 天前
用SQL求解advent of code 2024年23题
数据库·sql·算法