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

相关推荐
Jerry说前后端37 分钟前
RecyclerView 性能优化:从原理到实践的深度优化方案
android·前端·性能优化
武昌库里写JAVA2 小时前
JAVA面试汇总(四)JVM(一)
java·vue.js·spring boot·sql·学习
老虎06274 小时前
数据库基础—SQL语句总结及在开发时
数据库·sql·oracle
Mr. zhihao6 小时前
SQL LEFT JOIN 与 WHERE 条件的隐藏坑
数据库·sql
代码的余温7 小时前
MySQL性能优化:10个关键参数调整指南
数据库·mysql·性能优化
silver98868 小时前
sql链接的url中serverTimezone的作用
数据库·sql
小遁哥8 小时前
也是用上webworker了
react.js·性能优化
张柏慈9 小时前
JavaScript性能优化30招
开发语言·javascript·性能优化
sleetdream9 小时前
Flink Sql 按分钟或日期统计数据量
sql·flink
Apple_羊先森11 小时前
Oracle数据库操作深入研究:备份、数据删除与性能优化
数据库·oracle·性能优化