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

相关推荐
Decay__13 小时前
18_MySQL 智能巡检方案 —— 巡检项清单 / 一键巡检脚本 / 健康评分
运维·数据库·sql·mysql·架构
只睡四小时14 小时前
Three.js Sub-tick:漏判 60.7%→0
开发语言·javascript·性能优化·ecmascript·游戏开发·three.js·碰撞检测
凤凰院凶涛QAQ14 小时前
《MySQL内核拆解:从SQL到InnoDB》(四) : 联合查询 & 视图
数据库·sql·mysql
baiduxiaoyu021 天前
图片管线:一张商品图从 1.2MB 到 58KB
性能优化
贾伟康1 天前
【共创稿事节】HarmonyOS 7.0 BindContentCover全屏模态:从接入到生产验证
性能优化·实战·harmonyos·arkts·arkui
deepseek231 天前
Python日志拖慢请求?QueueHandler排队、丢失与退出实测
python·性能优化·logging·后端开发·queuehandler
贾伟康1 天前
【共创稿事节】HarmonyOS 7.0 Menu菜单与上下文:从接入到生产验证
性能优化·实战·harmonyos·arkts·arkui
qq_410194291 天前
.NET 性能优化:先找瓶颈,再谈技巧
性能优化·.net
落樱弥城1 天前
Perf 逐步探查性能缺口并优化
c++·性能优化
Decay__1 天前
19_MySQL 告警与自动化运维 —— 告警分级收敛 / Webhook / 定时巡检 / 脚本库
运维·数据库·sql·mysql·架构