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

相关推荐
e***13622 小时前
MySQL 常用 SQL 语句大全
数据库·sql·mysql
山岚的运维笔记4 小时前
SQL Server笔记 -- 第53章:INSERT 第54章:主键
数据库·笔记·sql·microsoft·sqlserver
SQL必知必会4 小时前
SQL 聚合函数详解:COUNT、SUM、AVG、MIN、MAX
数据库·sql
御坂10101号5 小时前
从暴力扫图到成本估算:SpiceDB 如何重构 ReBAC 性能引擎
算法·性能优化·架构·database
SQL必知必会5 小时前
使用 SQL 实现帕累托原则(80/20 法则)
大数据·数据库·sql
q***76565 小时前
MySQL 中如何进行 SQL 调优
android·sql·mysql
weixin199701080167 小时前
Lazada商品详情页前端性能优化实战
前端·性能优化
御坂10101号9 小时前
JIT 上的 JIT:Elysia JS 的优化实践与争议
开发语言·javascript·网络·性能优化·node.js·express
m***495811 小时前
LangChain-08 Query SQL DB 通过GPT自动查询SQL
数据库·sql·langchain