Oracle table join study notes

A table type connector

Table connection types can be divided into: inner join, outer join, looking at the "harvest, more than sql optimization," a book and made notes

Within 1.1 connection

The connection: connection means result table includes only those records satisfy the join condition. Here to learn about the inner join, give an example, here created two tables, and then query the inner connection, look at an example:

SQL>select * from t1;
id   col1
---- ----
  A    A1
  B    B1
  C    C1
  D    D1
SQL>select * from t2;
id    col2
---- ----
  A    A2
  C    B2
  D    C2
  E    D2
SQL>select * from t1,t2 where t1.id=t2.id;
id   col1 col2
---- ---- ----
 A    A1    A2
 C    C1    C2
 D    D1    D2

Criteria: SQL left outer join is not defined outside of the connector, right outer join, full outer join, and (+) compliance, which is called SQL uniform within the connector, so there can not be identified sql SQL outer join, or the entire sql external connection have become

Oracle (+) sign usage:

Oracle connection may use left and right (+), + sign indicates the left and right outer join, the right indicates the outer left connection

Examples are within the following sql connection:

t1, t2 method

select * from t1,t2 where t1.id = t2.id;

inner join on方法

select * from t1 inner join t2 on t1.id = t2.id

inner keyword can be omitted

select * from t1 join t2 on (t1.id=t2.id);

join using方法

select * from t1 join t2 using(id);

1.2 an outer connector

External connection: external connection is connected internally to expand, it refers to a record in full compliance with the outside, but also contains a record of all connected drives table does not meet the conditions of

Left situation connected

SQL>select * from t1;
id   col1
---- ----
  A    A1
  B    B1
  C    C1
  D    D1
SQL>select * from t2;
id    col2
---- ----
  A    A2
  C    B2
  D    C2
  E    D2
SQL>select * from t1 left join t2 on t1.id=t2.id;
id   col1 col2
---- ---- ----
 A    A1    A2
 B    B1    
 C    C1    C2
 D    D1    D2

The right connection, in turn, is driven to prevail table t2; full outer join is, whether driving or being driven table table t1 t2 all check out, regardless of whether the connection conditions, the syntax is full join on

Second, the method of connection tables

Table 2.1 Classification connection method

Table connection method between the two tables with a sort-merge join, nested loop join, hash join, connect Cartesian

  • Sort-merge join (merge sort join)

  • Nested loop join (Nested loop join)

  • Hash join (Hash join)

  • Cartesian connections (Cross join)

Table 2.2 Characteristics of the connection method distinction

(1) the difference between the number of table accesses

Use Hint syntax enforces nl

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

View the execution plan

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

PLAN_TABLE_OUTPUT

SQL_ID  245z7n1cxaf3m, child number 0
-------------------------------------
SELECT /*+ leading(t1) use_nl(t2)*/ * FROM t1, t2 WHERE t1.id = t2.t1_id

Plan hash value: 1967407726

--------------------------------------------------------------------------------
-----
| Id  | Operation          | Name | Starts | E-Rows | A-Rows |   A-Time   | Buff
ers |
--------------------------------------------------------------------------------
-----
|   0 | SELECT STATEMENT   |      |      1 |        |    300 |00:00:00.25 |   29
747 |
|   1 |  NESTED LOOPS      |      |      1 |    300 |    300 |00:00:00.25 |   29
747 |
|   2 |   TABLE ACCESS FULL| T1   |      1 |    300 |    300 |00:00:00.01 |
 27 |
|*  3 |   TABLE ACCESS FULL| T2   |    300 |      1 |    300 |00:00:00.25 |   29
720 |
--------------------------------------------------------------------------------
-----

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

   3 - filter("T1"."ID"="T2"."T1_ID")

Note

PLAN_TABLE_OUTPUT

   - dynamic sampling used for this statement (level=2)


已选择24行。

Nested sort join, the drive table is accessed 0 or 1, the table is accessed driven 0 or n times, n being driven table returns a result set number of

Then can also be practiced hash join, merge join the, hash join with / * + leading (t1) use_hash (t2) * /

Hash join the drive table is accessed to 0 or 1, the same table is driven

merge sort join the drive table is accessed to 0 or 1, the same table is driven

(2) Effect of connect order

T1 is the case with the foregoing table driven, now a change order,

SQL>SELECT /*+ leading(t2) use_nl(t1)*/ * FROM t1, t2 WHERE t1.id = t2.t1_id;
SQL> select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

PLAN_TABLE_OUTPUT

SQL_ID  fgw5v7y16yn4m, child number 0
-------------------------------------
SELECT /*+ leading(t2) use_nl(t1)*/ * FROM t1, t2 WHERE t1.id = t2.t1_id

Plan hash value: 4016936828

--------------------------------------------------------------------------------
-----
| Id  | Operation          | Name | Starts | E-Rows | A-Rows |   A-Time   | Buff
ers |
--------------------------------------------------------------------------------
-----
|   0 | SELECT STATEMENT   |      |      1 |        |    300 |00:00:00.30 |   70
139 |
|   1 |  NESTED LOOPS      |      |      1 |    300 |    300 |00:00:00.30 |   70
139 |
|   2 |   TABLE ACCESS FULL| T2   |      1 |   9485 |  10000 |00:00:00.01 |
119 |
|*  3 |   TABLE ACCESS FULL| T1   |  10000 |      1 |    300 |00:00:00.29 |   70
020 |
--------------------------------------------------------------------------------
-----

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

   3 - filter("T1"."ID"="T2"."T1_ID")

Note

PLAN_TABLE_OUTPUT

   - dynamic sampling used for this statement (level=2)


已选择24行。

As can be seen the connection order table NL connection is influential, experimental Similarly, it can be seen on the hash join it is influential, but does not affect the merger join

(3) ordering the connection table

For these types of table joins, you can view the properties of sorts with the set autotrace on the way, we can draw only merge join is sorted, Nl connection and hash join are unordered

(4) failure of the joined tables where

hash join unsupported condition ">, <, <>, like" connections, merge join unsupported condition "<>, like" support "<,>" situation, and nl is connected is not limited, it table distinction is one of several methods of connection

Guess you like

Origin blog.csdn.net/u014427391/article/details/93461506