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

相关推荐
zgscwxd10 小时前
B审批流程与数据库设计要点
sql
椰猫子10 小时前
数据库(数据库相关概念、MySQL数据库、SQL(DDL、DML、DQL))
数据库·sql·mysql
喵了几个咪11 小时前
MySQL 运维实战:ibd 文件批量转换为 SQL 完整指南(基于 ibd2sql)
运维·sql·mysql
CappuccinoRose11 小时前
关系数据库标准语言(SQL)- 软考备战(三十一)
数据库·sql·软考
山峰哥12 小时前
解锁SQL优化新境界:从索引策略到高效查询实战
数据库·sql·oracle
野生技术架构师12 小时前
从两套系统到一条 SQL:SelectDB search() 搞定日志的搜索与分析
数据库·sql
qq_120840937112 小时前
Three.js 场景性能优化实战:首屏、帧率与内存的工程化治理
开发语言·javascript·性能优化·three.js
一只大袋鼠13 小时前
JDBC 详细笔记:从基础 API 到 SQL 注入解决
数据库·笔记·sql·mysql
MU在掘金9169513 小时前
给Perfetto trace装上GPS:我设计了一套源码级性能归因系统
性能优化
|晴 天|13 小时前
我的博客性能优化实战:首屏加载从2.5s降到1.2s
性能优化