Oracle中关于not in的替代方案

Oracle优化连接查询速度

今天在使用dblink的时候,多表关联时发现条件中使用 not in 作为条件,会极大的影响查询速度,尤其是not in中的表数据量很大时,简直是一种灾难;经过翻阅资料,找到两种比较好用的方法:

  1. 采用exists模式
  2. 采用Left join模式

样例

sql 复制代码
select columns from tab1 
where column1 not in (select column1 from tab2);

exists模式

sql 复制代码
select columns from tab1 where 
not exists(select 1 from tab2 where tab1.column1 = tab2.column1);

exists返回true或false,具体的问题研究建议找一些讲的比较深入的资料,本人才疏学浅,解释不清

Left join模式

sql 复制代码
select columns from tab1 
left join tab2 on tab1.column1 = tab2.column1
where tab2.column1 is null

左连接,以左表为主,副表tab2中 关联不到的就自动赋值null

不理解可以查下左连接关联的资料画画图
注: is null 可能有坑,建议使用 nvl(trim(tab2.column1),'自己指定值') = '自己指定值'

相关推荐
zuoerjinshu3 小时前
sql实战解析-sum()over(partition by xx order by xx)
数据库·sql
NocoBase5 小时前
【2.0 教程】第 1 章:认识 NocoBase ,5 分钟跑起来
数据库·人工智能·开源·github·无代码
Hoshino.416 小时前
基于Linux中的数据库操作——下载与安装(1)
linux·运维·数据库
Oueii7 小时前
Django全栈开发入门:构建一个博客系统
jvm·数据库·python
未来龙皇小蓝8 小时前
【MySQL-索引调优】11:Group by相关概念
数据库·mysql·性能优化
2401_831824968 小时前
使用Fabric自动化你的部署流程
jvm·数据库·python
njidf8 小时前
Python日志记录(Logging)最佳实践
jvm·数据库·python
twc8298 小时前
大模型生成 QA Pairs 提升 RAG 应用测试效率的实践
服务器·数据库·人工智能·windows·rag·大模型测试
@我漫长的孤独流浪8 小时前
Python编程核心知识点速览
开发语言·数据库·python
2401_851272998 小时前
实战:用Python分析某电商销售数据
jvm·数据库·python