Oracle 左外连接的一些測试

Posted yfceshi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Oracle 左外连接的一些測试相关的知识,希望对你有一定的参考价值。

  为了更加深入左外连接,我们做一些測试,外连接的写法有几种形式,我们能够通过10053跟踪到终于SQL转换的形式。

--初始化数据

create table A

(
  id number,
  age number
);
create table b
(
  id number,
  age number
);
insert into A values(1,10);
insert into A values(2,20);
insert into A values(3,30);
insert into B values(1,10);
insert into B values(2,20);
commit;


--用10053找到终于转换后的SQL

alter session set session_cached_cursors =0;
alter session set events ‘10053 trace name context forever, level  1‘;
explain plan for select * from A left join B on A.id = B.id and A.age > 5;
explain plan for select * from A left join B on A.id = B.id WHERE A.age > 5;
explain plan for select * from A left join B on A.id = B.id and b.age > 5;
explain plan for select * from A left join B on A.id = B.id where b.age > 5;
alter session set events ‘10053 trace name context off‘ ;


select * from A left join B on A.id = B.id and A.age > 5;
        ID        AGE         ID        AGE
---------- ---------- ---------- ----------
         1         10          1         10
         2         20          2         20
         3         30
--Final query after transformations:
SELECT "A"."ID" "ID", "A"."AGE" "AGE", "B"."ID" "ID", "B"."AGE" "AGE"
  FROM "GG_TEST"."A" "A", "GG_TEST"."B" "B"
 WHERE "A"."ID" = "B"."ID"(+)
   AND "A"."AGE" > CASE WHEN("B"."ID"(+) IS NOT NULL) THEN 5 ELSE 5 END


select * from A left join B on A.id = B.id WHERE A.age > 5;
        ID        AGE         ID        AGE
---------- ---------- ---------- ----------
         1         10          1         10
         2         20          2         20
         3         30
--Final query after transformations:
SELECT "A"."ID" "ID", "A"."AGE" "AGE", "B"."ID" "ID", "B"."AGE" "AGE"
  FROM "GG_TEST"."A" "A", "GG_TEST"."B" "B"
 WHERE "A"."AGE" > 5
   AND "A"."ID" = "B"."ID"(+);


select * from A left join B on A.id = B.id and b.age > 5; 
        ID        AGE         ID        AGE
---------- ---------- ---------- ----------
         1         10          1         10
         2         20          2         20
         3         30  
--Final query after transformations:
SELECT "A"."ID" "ID", "A"."AGE" "AGE", "B"."ID" "ID", "B"."AGE" "AGE"
  FROM "GG_TEST"."A" "A", "GG_TEST"."B" "B"
 WHERE "A"."ID" = "B"."ID"(+)
   AND "B"."AGE"(+) > 5

--这样的形式你能够看到外连接失效,CBO还是很聪明的
select * from A left join B on A.id = B.id where b.age > 5;

        ID        AGE         ID        AGE
---------- ---------- ---------- ----------
         1         10          1         10
         2         20          2         20
--Final query after transformations:
SELECT "A"."ID" "ID", "A"."AGE" "AGE", "B"."ID" "ID", "B"."AGE" "AGE"
  FROM "GG_TEST"."A" "A", "GG_TEST"."B" "B"
 WHERE "B"."AGE" > 5
   AND "A"."ID" = "B"."ID";












































































以上是关于Oracle 左外连接的一些測试的主要内容,如果未能解决你的问题,请参考以下文章

ORACLE 左外连接问题(有空表?)

oracle 左外连接如果右表中有重复数据如何处理?

oracle左外连接不显示右空值

oracle的SQL语句中的(+) 左外连接

添加 2 表条件时,Oracle 左外连接停止返回结果

如何在 Oracle 中转换“旧版”左外连接语句?