oracle表連線(二)

2021-06-28 13:47:24 字數 1690 閱讀 2581

各類連線驅動順序區別

1.巢狀迴圈連線

select /*+leading(t1) use_nl(t2)*/ * from t1, t2 where t1.id = t2.t1_id and t1.n = 19;

select /*+leading(t2) use_nl(t1)*/ * from t1, t2 where t1.id = t2.t1_id and t1.n = 19;

select * from table(dbms_xplan.display_cursor(null, null, 'allstats last'));

t1表先訪問:buffer 1014,t2只訪問1次

t2表先訪問:buffer 701k,t1被訪問100000次

所以,巢狀迴圈連線要特別注意驅動表的順序,小的結果集先訪問,大的結果集後訪問

2. 雜湊連線

select /*+leading(t1) use_hash(t2)*/ * from t1, t2 where t1.id = t2.t1_id and t1.n = 19;

select /*+leading(t2) use_hash(t1)*/ * from t1, t2 where t1.id = t2.t1_id and t1.n = 19;

select * from table(dbms_xplan.display_cursor(null, null, 'allstats last'));

t1表先訪問:buffer 1013,used_mem 286k,時間0.04秒

t2表先訪問:buffer 1013,used_mem 11mb,時間0.01秒

在雜湊連線中,驅動表順序也很重要

3.排序合併連線

select /*+leading(t1) use_merge(t2)*/ * from t1, t2 where t1.id = t2.t1_id and t1.n = 19;

select /*+leading(t2) use_merge(t1)*/ * from t1, t2 where t1.id = t2.t1_id and t1.n = 19;

select * from table(dbms_xplan.display_cursor(null, null, 'allstats last'));

無論先訪問t1還是先訪問t2,效率都一樣,執行時間,buffer,used_mem都一樣,這表明,排序合併連線沒有驅動表概念。

綜上所述:巢狀迴圈連線和雜湊連線有驅動順序,驅動表的順序不同將影響表連線的效能;而排序合併連線沒有驅動的概念,無論哪張表在前都無妨。

巢狀迴圈連線不需要排序;雜湊連線並不排序,消耗記憶體是用於建議hash表;排序合併連線需要排序。關於雜湊連線和排序合併連線,不要取多餘的字段參與排序,因為選擇的字段越少,消耗記憶體的尺寸就越小。

select /*+leading(t2) use_merge(t1)*/ * from t1, t2 where t1.id = t2.t1_id and t1.n = 19;

select /*+leading(t2) use_merge(t1)*/ t1.id from t1, t2 where t1.id = t2.t1_id and t1.n = 19;

select * from table(dbms_xplan.display_cursor(null, null, 'allstats last'));

可以看出used_mem代表的記憶體消耗差別很大

Oracle表連線方式

實用記憶方法 在哪邊,哪邊就是外來鍵,該錶的資料就 少 無 那邊檢索的就是全表資料。oracle 8i,9i 表連線方法。一般的相等連線 select from a,b where a.id b.id 這個就屬於內連線。對於外連線 oracle中可以使用 來表示,9i可以使用left right f...

oracle表的連線

表連線操作有內連線和外連線兩種 1 內連線 也稱等值連線或自然連線,內連線是從結果表中刪除與其他被連線表中沒有匹配行的所有元組,所以當匹配條件不滿足時內連線可能會丟失資訊。例如 select from emp e,dept d where e.deptno d.deptno 2 外連線 外連線分為三...

oracle 表連線方式

一 連線方式 巢狀迴圈 nested loops nl 雜湊 雜湊連線 hash join hj 歸併 排序合併連線 sort merge join smj 二 連線說明 1.oracle一次只能連線兩個表。不管查詢中有多少個表,oracletable 和內部表 inner table 在巢狀迴圈連...