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

相关推荐
ss2734 小时前
食谱推荐系统功能测试如何写?
java·数据库·spring boot·功能测试
l1t4 小时前
DeepSeek总结的数据库外部表
数据库
m0_674294644 小时前
如何编写SQL存储过程性能对比_记录执行时间评估优化效果
jvm·数据库·python
014-code4 小时前
CompletableFuture 实战模板(超时、组合、异常链处理)
java·数据库
运气好好的5 小时前
怎样开启phpMyAdmin的操作审计日志_记录每条执行的SQL
jvm·数据库·python
それども5 小时前
DELETE 和 TRUNCATE TABLE区别
java·数据库·mysql
wenha5 小时前
数据库隔离级别
数据库·mysql·sqlserver·隔离级别
2401_871492855 小时前
Layui如何修改Layui默认的UI主题颜色(换肤功能实现)
jvm·数据库·python
Edward111111116 小时前
4.27mysql ,数据库,数据源
数据库·mysql
小徐敲java6 小时前
踩坑实录:MySQL8.0 导入SQL报错 2006 - MySQL server has gone away 完美解决
数据库·sql