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

相关推荐
Lei活在当下6 小时前
【Perfetto从入门到精通】2. 使用 Perfetto 追踪/分析 APP 的 Native/Java 内存
android·性能优化·架构
Knight_AL11 小时前
MySQL STORED 生成列(Generated Column)详解:让 SQL 变快的秘密武器
数据库·sql·mysql
脸大是真的好~12 小时前
尚硅谷-mysql专项训练-数据库服务的优化-慢查询-EXPLAIN字段
数据库·mysql·性能优化
Dragon online12 小时前
数据分析师成长之路--从SQL恐惧到数据掌控者的蜕变
数据库·sql
Red Car13 小时前
虚拟机性能优化实战技术
性能优化
luguocaoyuan13 小时前
JavaScript性能优化实战技术学习大纲
开发语言·javascript·性能优化
Li.CQ14 小时前
SQL学习笔记
笔记·sql·学习
better_liang14 小时前
每日Java面试场景题知识点之-Elasticsearch
java·elasticsearch·搜索引擎·面试·性能优化
Navicat中国14 小时前
Navicat 技术指引 | 面向达梦的查询工具
数据库·sql·达梦·navicat·查询
LYFlied15 小时前
前端性能优化:成本收益权衡下的实践路径
前端·网络·面试·性能优化·打包构建·页面加载链路