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

相关推荐
我真的是大笨蛋1 小时前
Redo Log详解
java·数据库·sql·mysql·性能优化
Root_Hacker3 小时前
sql注入学习笔记
数据库·sql·web安全·网络安全·oracle·网络攻击模型
hamawari4 小时前
SQL语法
数据库·sql·oracle
heartbeat..4 小时前
Redis 性能优化全指南:从基础配置到架构升级
java·redis·性能优化·架构
醉风塘5 小时前
JDBC批量操作终极指南:PreparedStatement批处理与事务性能优化实战
数据库·性能优化
invicinble5 小时前
对于mysql层对sql层面的知识体系的理解和把握
数据库·sql·mysql
听麟6 小时前
HarmonyOS 6.0+ PC端多人联机游戏开发实战:Game Service Kit深度集成与跨设备性能优化
游戏·华为·性能优化·架构·harmonyos·ai-native
l1t6 小时前
DeepSeek辅助总结postgresql wiki提供的数独求解器
数据库·sql·postgresql
万行6 小时前
SQL进阶&索引篇
开发语言·数据库·人工智能·sql