程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> Oracle數據庫 >> Oracle教程 >> Oracle左外連接的一些測試

Oracle左外連接的一些測試

編輯:Oracle教程

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";

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved