如何在 Oracle SQL 中强制加入浓密的树?

tr3*_*sta 3 oracle

据我所知,默认连接是深左连接。如何在 4 张桌子上强制加入浓密的树?

Bal*_*app 5

好吧,如果你真的想这样做,这是一种可能的方法:

create table t1 (id number, c1 number);
create table t2 (id number, c2 number);
create table t3 (id number, c3 number);
create table t4 (id number, c4 number);
Run Code Online (Sandbox Code Playgroud)

原来的:

select
  t1.id, t1.c1, t2.c2, t3.c3, t4.c4
from
  t1, t2, t3, t4
where
  t1.id = t2.id and t2.id = t3.id and t3.id = t4.id;

Execution Plan
----------------------------------------------------------
Plan hash value: 3021121231

------------------------------------------------------------------------------
| Id  | Operation             | Name | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------
|   0 | SELECT STATEMENT      |      |     1 |   104 |     8   (0)| 00:00:01 |
|*  1 |  HASH JOIN            |      |     1 |   104 |     8   (0)| 00:00:01 |
|   2 |   MERGE JOIN CARTESIAN|      |     1 |    78 |     6   (0)| 00:00:01 |
|*  3 |    HASH JOIN          |      |     1 |    52 |     4   (0)| 00:00:01 |
|   4 |     TABLE ACCESS FULL | T1   |     1 |    26 |     2   (0)| 00:00:01 |
|   5 |     TABLE ACCESS FULL | T2   |     1 |    26 |     2   (0)| 00:00:01 |
|   6 |    BUFFER SORT        |      |     1 |    26 |     4   (0)| 00:00:01 |
|   7 |     TABLE ACCESS FULL | T4   |     1 |    26 |     2   (0)| 00:00:01 |
|   8 |   TABLE ACCESS FULL   | T3   |     1 |    26 |     2   (0)| 00:00:01 |
------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - access("T2"."ID"="T3"."ID" AND "T3"."ID"="T4"."ID")
   3 - access("T1"."ID"="T2"."ID")
Run Code Online (Sandbox Code Playgroud)

浓密的:

with
  ta as (select /*+ no_merge */ t1.id, t1.c1, t2.c2 from t1, t2 where t1.id = t2.id),
  tb as (select /*+ no_merge */ t3.id, t3.c3, t4.c4 from t3, t4 where t3.id = t4.id)
select
    ta.id, ta.c1, ta.c2, tb.c3, tb.c4
from
  ta, tb
where
  ta.id = tb.id
;

Execution Plan
----------------------------------------------------------
Plan hash value: 975173817

-----------------------------------------------------------------------------
| Id  | Operation            | Name | Rows  | Bytes | Cost (%CPU)| Time     |
-----------------------------------------------------------------------------
|   0 | SELECT STATEMENT     |      |     1 |    78 |     8   (0)| 00:00:01 |
|*  1 |  HASH JOIN           |      |     1 |    78 |     8   (0)| 00:00:01 |
|   2 |   VIEW               |      |     1 |    39 |     4   (0)| 00:00:01 |
|*  3 |    HASH JOIN         |      |     1 |    52 |     4   (0)| 00:00:01 |
|   4 |     TABLE ACCESS FULL| T1   |     1 |    26 |     2   (0)| 00:00:01 |
|   5 |     TABLE ACCESS FULL| T2   |     1 |    26 |     2   (0)| 00:00:01 |
|   6 |   VIEW               |      |     1 |    39 |     4   (0)| 00:00:01 |
|*  7 |    HASH JOIN         |      |     1 |    52 |     4   (0)| 00:00:01 |
|   8 |     TABLE ACCESS FULL| T3   |     1 |    26 |     2   (0)| 00:00:01 |
|   9 |     TABLE ACCESS FULL| T4   |     1 |    26 |     2   (0)| 00:00:01 |
-----------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - access("TA"."ID"="TB"."ID")
   3 - access("T1"."ID"="T2"."ID")
   7 - access("T3"."ID"="T4"."ID")
Run Code Online (Sandbox Code Playgroud)