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

相关推荐
Access开发易登软件43 分钟前
Access链接Azure SQL
数据库·后端·sql·flask·vba·azure·access
John_ToDebug2 小时前
chrome源码中WeakPtr 跨线程使用详解:原理、风险与最佳实践
c++·chrome·性能优化
艺杯羹5 小时前
深入解析应用程序分层及 BaseDao 的封装策略
数据库·sql·mysql·jdbc·应用分层
码观天工5 小时前
C#线程池核心技术:从原理到高效调优的实用指南
性能优化·c#·.net·线程·线程池·多线程·异步
勤劳的进取家6 小时前
论文阅读:Next-Generation Database Interfaces:A Survey of LLM-based Text-to-SQL
数据库·sql
x-cmd8 小时前
[250521] DBeaver 25.0.5 发布:SQL 编辑器、导航器全面升级,新增 Kingbase 支持!
数据库·sql·编辑器·kingbase·dbeaver
Uranus^8 小时前
深入解析Spring Boot与Redis的缓存集成实践
java·spring boot·redis·缓存·性能优化
星辰离彬10 小时前
线上 Linux 环境 MySQL 磁盘 IO 高负载深度排查与性能优化实战
linux·运维·服务器·数据库·mysql·性能优化
John_ToDebug17 小时前
Chromium 浏览器核心生命周期剖析:从 BrowserProcess 全局管理到 Browser 窗口实例
c++·chrome·性能优化