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

相关推荐
前方一片光明3 小时前
SQL SERVER——生成sql:删除所有log表中,user_name是某用户的数据
数据库·sql·oracle
Gauss松鼠会4 小时前
【GaussDB】在duckdb中查询GaussDB的数据
数据库·sql·database·gaussdb
〝七夜5696 小时前
SQL之sql注入
sql
利刃大大9 小时前
【MyBatis】MyBatis操作动态sql && MyBatisGenerator
数据库·sql·mybatis
一瓢西湖水10 小时前
loader命令导出大批量数据维护SQL
数据库·sql
为码消得人憔悴12 小时前
Android perfetto - 记录分析memory
android·性能优化
yiyeguzhou10013 小时前
论文解读:Nomad: Non-Exclusive Memory Tiering viaTransactional Page Migration
性能优化
UWA16 小时前
GPM 2.0全新功能发布|GPU精准监测 + 精细化运营,重构游戏性能管控新范式
人工智能·游戏·性能优化·重构·游戏开发·uwa
Elastic 中国社区官方博客16 小时前
Elasticsearch:使用 ES|QL 与 dense_vector 字段
大数据·数据库·人工智能·sql·elasticsearch·搜索引擎·全文检索