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

相关推荐
名字还没想好☜19 小时前
Go 内存逃逸分析:什么时候变量跑到堆上
开发语言·算法·性能优化·golang·go·内存逃逸
●VON20 小时前
HarmonyKit | 鸿蒙新特性:HDS 沉浸光感与空间化视觉设计
华为·性能优化·单元测试·harmonyos·鸿蒙
Csvn20 小时前
📊 SQL 入门 Day 1:SELECT & WHERE — 数据查询的基本功
后端·sql
苍狼唤21 小时前
SQL+C# 增删改查(练习)
数据库·sql·c#
●VON21 小时前
HarmonyKit | 移动端开发者工具类应用的现状与趋势
git·华为·性能优化·harmonyos·鸿蒙
2301_8002561121 小时前
第七章 空间存储与索引 知识点梳理-2
数据库·sql·oracle
山峰哥1 天前
数据库工程与索引策略实战指南‌
服务器·数据库·sql·oracle·深度优先
ujainu小2 天前
并发性能测试:心跳监测与主线程阻塞分析
华为·性能优化·harmonyos
喜欢的名字被抢了2 天前
MySQL核心机制:事务、锁与存储引擎
数据库·sql·mysql·教程
JouYY2 天前
聊一下复杂 Agent 架构的性能优化设计
性能优化·llm·agent