oracle ORA-01417多表左外连接

drop table test1 purge;
drop table test2 purge;
drop table test3 purge;
create table test1 (id number primary key);
create table test2 (id number);
create table test3 (dept_id number,measure_id number);
insert into test1 values(1);
insert into test1 values(2);
insert into test1 values(3);
insert into test2 values(10);
insert into test2 values(20);
insert into test3 values(1,10);
commit;

select *
  from test1, test2, test3
 where test1.id = test3.dept_id(+)
   and test2.id = test3.measure_id(+);      
在11g会报错:
在12c是支持的

这里写图片描述
解决方式:

方式1:先合并
select *
  from (select test1.id dept_id, test2.id measure_id from test1, test2) a,
       test3
 where a.dept_id = test3.dept_id(+)
   and a.measure_id = test3.measure_id(+);
方式2:ansi语法
--横向视图是一个视图,它引用来自不在视图中的表中的列。
--在oracle11g执行这种查询的唯一方法是将其转换为ANSI语法。但是,这种ANSI语法的实现会导致使用横向视图。
--Oracle无法合并横向视图,因此优化器的计划选择受到连接顺序和联接方法的限制,这可能导致不是最优计划
select *
  from test1 cross join test2 
  left join test3
    on (test1.id = test3.dept_id and test2.id = test3.measure_id);  

看下执行计划:
这里写图片描述
上面的2种方式都是一样的执行计划,
这里可以看到,id=2的视图在oracle11g是无法合并的;这就可能会丢失最优的执行计划
在12C则可以合并;

参考:https://www.2cto.com/database/201411/355983.html

猜你喜欢

转载自blog.csdn.net/u011165335/article/details/80977211