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

相关推荐
Dreamboat¿8 分钟前
SQL 注入漏洞
数据库·sql
Huanzhi_Lin40 分钟前
关于V8/MajorGC/MinorGC——性能优化
javascript·性能优化·ts·js·v8·新生代·老生代
空中海5 小时前
2.7 列表与滚动性能优化
flutter·性能优化·dart
StackNoOverflow5 小时前
Spring Security权限控制框架详解
java·数据库·sql
一江寒逸7 小时前
零基础从入门到精通MySQL(下篇):精通篇——吃透索引底层、锁机制与性能优化,成为MySQL实战高手
数据库·mysql·性能优化
wang09078 小时前
Linux性能优化之中断
linux·运维·性能优化
databook8 小时前
逃离SQL丛林:实用主义的数据救赎
后端·sql·数据分析
流觞 无依9 小时前
DedeCMS plus/comment.php 评论 XSS/注入(XSS、SQL注入)修复教程
sql·php·xss
流觞 无依10 小时前
DedeCMS plus/recommend.php SQL注入漏洞修复教程
sql·php
流觞 无依11 小时前
DedeCMS plus/list.php 参数注入(SQL注入)修复教程
sql·安全·php