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

相关推荐
Wellijohn2 小时前
dict、tuple、dataclass 还是 slots?我把 6 种数据载体的 8 项开销全测了一遍
后端·python·性能优化
王二蛋与他的张大花6 小时前
后台内存 827MB,我一行业务逻辑都没改就砍到 100MB
android·flutter·性能优化
AI 编程助手GPT12 小时前
GPT 写 SQL:避免订单金额被重复统计
jvm·人工智能·sql·gpt
map_3d_vis13 小时前
异步加载百度地图:callback、Promise 封装和 jsapi-loader(JSAPI 4.0)
前端·javascript·性能优化·百度地图
جيون داد ناالام ميづ13 小时前
四、MyBatis 核心原理:拆解 7 大组件 + SQL 执行全流程
数据库·sql·mybatis
Escalating_xu14 小时前
【MySQL】视图特性:从创建与基表联动,到更新限制、权限与 SQL 实战
android·sql·mysql
智码看视界1 天前
开源大模型追踪:Snowflake Arctic 把企业 SQL 场景搬进自托管 MoE
sql·snowflake·moe·开源大模型·测评·企业ai·arctic
两点王爷2 天前
面试 | 索引是什么?有哪些分类?索引为什么会失效?
sql·面试
真上帝的左手2 天前
2. 编程语言-Java-Spring Boot-集成SkyWalking + Prometheus 性能工程闭环实战
java·spring boot·性能优化·架构·系统架构·prometheus·skywalking