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

相关推荐
二哈喇子!5 小时前
MySQL数据更新操作
数据库·sql
二哈喇子!5 小时前
MySQL命令行导入数据库
数据库·sql·mysql·vs code
学习中的DGR6 小时前
[GXYCTF2019]Ping Ping Ping 1和[SUCTF 2019]EasySQL 1新手解题过程
sql·安全·web安全·网络安全·php
田猿笔记14 小时前
用 PRQL 写查询,告别“SQL 咒语”:更简洁、可读、可组合的现代查询语言
sql
l1t14 小时前
DeepSeek对AliSQL 集成 DuckDB 的总结
数据库·sql·mysql·duckdb
踢足球092916 小时前
寒假打卡:2026-01-22
数据库·sql
黑夜中的潜行者16 小时前
构建高性能 WPF 大图浏览器:TiledViewer 技术解密
性能优化·c#·.net·wpf·图形渲染
小北方城市网16 小时前
生产级 Spring Boot + MyBatis 核心配置模板
java·spring boot·redis·后端·spring·性能优化·mybatis
jiayong2316 小时前
前端性能优化系列(二):请求优化策略
前端·性能优化
無森~16 小时前
Hive 函数
hive·hadoop·sql