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

相关推荐
Navicat中国1 小时前
AI 代码补全如何改变 DBA 编写 SQL 的方式
数据库·人工智能·sql·dba·navicat
H Journey3 小时前
C++性能优化
c++·性能优化
布吉岛的石头3 小时前
ClickHouse性能优化:OLAP数据库实战,让查询飞起来
数据库·clickhouse·性能优化
Java成神之路-4 小时前
解析 MyBatis 中 #{} 与 ${}区别及 SQL 注入防范(附 Like/In/Order by 安全写法)
sql·安全·mybatis
会编程的土豆4 小时前
Gin 框架第一课:从 0 搞懂 Gin 最基础的路由
数据库·sql·gin·goland
雅俗数据库5 小时前
OCP实验 | 03-SQL优化
数据库·sql
空空潍5 小时前
MySQL存储引擎与索引深度解析
后端·sql·mysql·innodb
Java成神之路-5 小时前
理解预编译 SQL:防注入原理、局限性
sql
天天进步20156 小时前
魔音漫创源码解析:性能优化: Electron 环境下的图片管理与文件系统协议处理优化
javascript·性能优化·electron
Zephyr_06 小时前
SQL,MyBatis-Plus,maven,Spring与VUE3
sql·spring·vue·maven·mybatis