Oracle 左外连接的一些测试
为了更加深入左外连接,我们做一些测试,外连接的写法有几种形式,我们可以通过10053跟踪到最终SQL转换的形式。
为了更加深入左外连接,我们做一些测试,,外连接的写法有几种形式,我们可以通过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锁表查询
推荐阅读
-
清晰讲解SQL语句中的外连接,通用于Mysql和Oracle,全是干货哦
-
清晰讲解SQL语句中的外连接,通用于Mysql和Oracle,全是干货哦
-
Oracle 左连接(+)加号用法及常用语法之间的关系
-
Oracle左连接、右连接、全外连接以及(+)号用法
-
SQL的四种连接-左外连接、右外连接、内连接、全连接
-
Oracle dblink的连接模式的关系测试总结
-
oracle教程之Oracle的多表查询举例(笛卡尔集、等值与非等值连接、外连接、自连接、SQL1999连接、Oracle的set运算符)实例
-
oracle的自连接、内连接、外连接、交叉连接、自然连接、等值连接、不等值连接
-
MySQL --- 左连接、右连接、内连接和全外连接的区别
-
Oracle表连接的内连接、外连接