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

相关推荐
小韩博13 分钟前
小迪第42课:PHP应用&MYSQL架构&SQL注入&跨库查询&文件读写&权限操作
sql·mysql·网络安全·架构·php
Sthenia1 小时前
如何用 Chrome DevTools 定位 Long Task:一份从零到实战的排查笔记
前端·性能优化
xinyu_Jina2 小时前
Calculator Game:WebAssembly在计算密集型组合优化中的性能优势
前端·ui·性能优化
猫豆~3 小时前
Ansible自动运维——6day
linux·数据库·sql·缓存·云计算
ssshooter4 小时前
为什么移动端 safari 用 translate 移动元素卡卡的
前端·css·性能优化
last_zhiyin4 小时前
Oracle sql tuning guide 翻译 Part 4-1 --- 连接操作(Joins)
数据库·sql·oracle
-suiyuan-4 小时前
sqli-labs靶场1~2笔记
数据库·sql
爱好读书5 小时前
AI生成ER图|SQL生成ER图
数据库·人工智能·sql·毕业设计·课程设计
Knight_AL6 小时前
视图 vs 临时表:它们的差异、适用场景与性能优化
性能优化
rannn_1116 小时前
【SQL题解】力扣高频 SQL 50题|DAY2+3
数据库·后端·sql·leetcode