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

相关推荐
ClouGence1 小时前
数据库 CI/CD 再升级:CloudDM 4.1.0 支持多数据库发布流编排
数据库·sql·ci/cd
阿里云大数据AI技术1 小时前
一套 Spark SQL,打通多种 Catalog:EMR Serverless Spark 统一数据处理实践
人工智能·sql·spark
今天AI了吗5 小时前
从“金鱼脑”到“大象记忆”:AI Agent 短期记忆与长期记忆的存储与检索全解
数据库·人工智能·python·sql·rust
养生技术人5 小时前
Oracle OCP认证考试题目详解082系列第16题
数据库·sql·oracle·ocp
k4m7v2pz6 小时前
Godot 4 仿 agar.io 卡顿优化:用空间网格把 30 万次碰撞检测降到几千次
性能优化·godot·游戏开发·碰撞检测·空间网格
Elastic 中国社区官方博客6 小时前
驯服 PUNKs:ES|QL 如何查询 Elasticsearch 从未被告知的字段
大数据·sql·elasticsearch·搜索引擎·全文检索
UWA6 小时前
GPM 2.0 WebGL 监测能力扩容|覆盖 Unity/Cocos/LayaAir 多引擎微信 & 抖音小游戏
unity·微信·性能优化·小游戏·webgl
存在morning7 小时前
【Flink SQL 学习笔记 二】实时聚合:用窗口和 Watermark 把订单按时段统计
sql·学习·flink
大模型码小白7 小时前
Spring AI 框架中集成 MCP 的完整指南:从服务端到客户端的全流程实践
大数据·运维·数据库·人工智能·python·sql·spring
Co_Hui7 小时前
include、merge、ViewStub与 性能优化
性能优化