Oracle syntax left joins three or more tables

后端 未结 2 412
后悔当初
后悔当初 2021-01-14 18:23

I am trying to wrap my head around the old oracle Left Join syntax. It\'s fine with two tables:

FROM A, B
WHERE
A.Col = B.Col (+)

(Lets cal

2条回答
  •  醉梦人生
    2021-01-14 19:19

    You can see it in a cascading way. However, the key is to look for those tables which are left and right joined within the same query. In this case, the order is different: the condition where the table is right joined is applied first. I hope the following diagram will shed some light on this:

    enter image description here

    You can also check the order of these joins by looking at the execution plan of the query:

    For Q1:

    select a.c a, b.c b, c.c c   from a, b, c  where a.c = b.c (+)    and
    c.c = a.c (+)
    
    ------------------------------------------------------------------------
    | Id  | Operation           | Name | E-Rows |  OMem |  1Mem | Used-Mem |
    ------------------------------------------------------------------------
    |   0 | SELECT STATEMENT    |      |        |       |       |          |
    |*  1 |  HASH JOIN OUTER    |      |      4 |  2168K|  2168K|  805K (0)|
    |*  2 |   HASH JOIN OUTER   |      |      4 |  2616K|  2616K|  981K (0)|
    |   3 |    TABLE ACCESS FULL| C    |      4 |       |       |          |
    |   4 |    TABLE ACCESS FULL| A    |      4 |       |       |          |
    |   5 |   TABLE ACCESS FULL | B    |      4 |       |       |          |
    ------------------------------------------------------------------------
    

    For Q2:

    select a.c a, b.c b, c.c c   from a, b, c  where c.c = a.c (+)    and
    a.c = b.c (+)
    
    ------------------------------------------------------------------------
    | Id  | Operation           | Name | E-Rows |  OMem |  1Mem | Used-Mem |
    ------------------------------------------------------------------------
    |   0 | SELECT STATEMENT    |      |        |       |       |          |
    |*  1 |  HASH JOIN OUTER    |      |      4 |  2168K|  2168K|  801K (0)|
    |*  2 |   HASH JOIN OUTER   |      |      4 |  2616K|  2616K|  983K (0)|
    |   3 |    TABLE ACCESS FULL| C    |      4 |       |       |          |
    |   4 |    TABLE ACCESS FULL| A    |      4 |       |       |          |
    |   5 |   TABLE ACCESS FULL | B    |      4 |       |       |          |
    ------------------------------------------------------------------------
    

提交回复
热议问题