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),'自己指定值') = '自己指定值'

相关推荐
Elastic 中国社区官方博客17 小时前
使用 TypeScript 创建 Elasticsearch MCP 服务器
大数据·服务器·数据库·人工智能·elasticsearch·搜索引擎·全文检索
jjjava2.017 小时前
数据库入门指南:核心操作与约束详解
数据库
华农DrLai17 小时前
知识工程和知识图谱有什么区别?如何构建完整的知识体系?
数据库·人工智能·gpt·nlp·prompt·知识图谱
ID_1800790547317 小时前
淘宝商品详情API的调用频率限制是多少?
大数据·数据库·json
2301_8042154117 小时前
Python类型提示(Type Hints)详解
jvm·数据库·python
一只努力的微服务17 小时前
【Calcite 系列】深入理解 Calcite 的 AggregateUnionTransposeRule
大数据·数据库·calcite·优化规则
2301_8166512217 小时前
用户认证与授权:使用JWT保护你的API
jvm·数据库·python
Sunshine for you17 小时前
Python单元测试(unittest)实战指南
jvm·数据库·python
研究点啥好呢17 小时前
3月28日Github热榜推荐 | 你还没有为AI接一个数据库吗
数据库·人工智能·驱动开发·github
草莓熊Lotso18 小时前
MySQL 多表连接查询实战:内连接 + 外连接
android·运维·数据库·c++·mysql