Oracle's connection details (left connection, right connection, full connection...)

https://www.cnblogs.com/guogl/articles/5929852.html

 

1 Description:

It is mentioned that the database must know the join operation. This part of the content plays a pivotal role in the database query operation. Today I will talk about these join operations.

The join operation can be specified in the WHERE or in the FROM clause. When specifying the join condition in the FROM clause, SQL2 divides the join operator into two parts: the join type and the join condition.

The join type determines how unmatched tuples in the join condition are handled.

2 Connection operation:

The join condition determines which elements in the two relations should match.

join type

illustrate

INNER JOIN

Inner join, the result is a join of matching rows from the two joined tables

LEFT OUTER JOIN

Left join: The result includes all rows in the left table (appearing at the far left of the JOIN clause), excluding unmatched rows in the right table.

RIGHT OUTER JOIN

Right join: The result includes all rows in the right table (appearing on the far right of the JOIN clause), excluding unmatched rows in the left table.

FULL OUTER JOIN

Full join: The result includes all rows in all joins, whether they match or not.

CROSS JOIN

Cross join: The result includes all possible combinations of rows from the two joined tables. A cross join returns the Cartesian product of the two tables. (Not supported by Oracle)

NATURAL JOIN

During natural join, it looks for fields with the same data type and column name in the two tables, and then automatically joins them.

In layman's terms: a table will be used as the base table for left join and right join, the contents of the table will be displayed in full, and then the matching contents of the two tables will be added.

        

This figure is referenced to: http://blog.csdn.net/caolaosanahnu/article/details/8080350

 

For outer joins, (+) can be used in Oracle to indicate, the way to use:

1. The (+) operator can only appear in the WHERE clause, and cannot be used simultaneously with the outer join syntax.

2. When using the (+) operator to perform an outer join, if you include multiple conditions in the where clause, you must include the (+) operator in all conditions.

3. The (+) operator can only be applied to columns, not expressions.

4. The (+) operator cannot be used with the or and in operators.

5. The (+) operator can only be used to implement left outer join and right outer join, but not to achieve full outer join.

 

Notes on natural connections:

(1) If there are multiple fields in the two tables that are naturally connected, they all have the same name and type, then they will all be used as the conditions for the connection.

(2) If only the field names in the two tables are the same, but the data types are different, an error will be returned at this time.

 

Here I will explain several connection methods, which are also used as supplementary knowledge:

1.  Self joins :

Using self-join, you can treat a mirror of your own table as another table, and query the data after connecting.

2.  Cartesian Products  Cartesian product

When there is no join operation between the two tables, what is the data obtained by querying these two tables? is the Cartesian product of these two tables.

 

3 Multi-table join operation

When performing a multi-table joint search, you will find that the multi-table will perform join operations, and these join operations include three ways:

Hash Join (hash join), Nested Loops, Sort Merge join, the understanding of these connection methods can help us to further understand the connection operation, and we will introduce them one by one.

3.1 HASH JOIN (hash join)

Hash Join is mainly a CBO (CBO: Cost-Based Optimization cost-based optimizer, one of the Oracle optimizers, which will be introduced later). It is a common way to perform join operations when the amount of data is relatively large. The optimizer The smaller of the two tables (smaller table or data source) will be selected, a hash table will be created in memory using the join key (JOIN KEY), the column data will be stored in the hash list, and then the larger table will be scanned. Match the JOIN KEY to the hash table to find the matching values. Note at this time: if the HASH table is too large and cannot be stored in memory at one time, it will be divided into several partitions (segments) and written to the temporary segment of the disk. At this time, the performance will be at the cost of one more write, which may reduce the efficiency.

This content is mainly applicable to smaller tables (that can fit in memory), where the relevant performance is the sum of the cost of accessing the performance of both.

Hash joins can be forced using the USE_HASH(table_name1 table_name2) hint.

3.2 SORT MERGE JOIN: sort merge join

Merge Join sort and merge join is to first sort the two associated tables according to the associated key (JOIN KEY), and then extract data from the respective sorted tables and match them in another sorted table.

Relatively speaking, merge join needs to complete the sorting operation, so it consumes more performance, but if the source has been sorted, it will achieve better performance. Applicable to: unequal association (>,<,>=,<=,<>), HASH_JOIN_ENABLED=false, etc.

You can use USE_MERGE(table_name1 table_name2) to force a sort-merge join.

3.3 NESTED LOOP: Nested Loop Join

Nested Loops nested loop joins work by looping to read data from one table (the drive table outer table), and then access another table (the inner table of the lookup table, hoping to have an index). Each row in the driver table is linked (JOIN) with the corresponding record in the inner table. The inner table is driven by the outer table, and each row in the outer table will be matched with the inner table, so the query result set should not be too large. Use the table with a small amount of data as the driver table (outer table) of the query, and you can use ordered to prompt the CBO default driver table.

Nested loop joins are relatively better when the drive table is small, and the inner table needs an efficient access index (index).

Use USE_NL(table_name1 table_name2) to force CBO to perform a nested loop join.

4 Examples:

Two tables of Oracle: department information table: DEPTINFO, user table: USERINFO

    

 

4.1 INNER JOIN connection operation:

The INNER JOIN connection is an equi-join, and its operation is equivalent to the wait-to-join in where.

 

 

4.2 LEFT JOIN connection operation:

USERINFO left join DEPTINFO, the result will be based on USERINFO table, its content will be displayed.

 

Oracle includes 3 connection methods: they are:

4.3 RIGHT JOIN link operation

USERINFO right joins DEPTINFO, and the result will take DEPTINFO as the base table and display all its contents.

 

4.4 FULL OUTER JOIN connection operation:

USERINFO is fully connected to DEPTINFO, and as a result, the contents of USERINFO and DEPTINFO are displayed.

  

4.5 NATURAL JION natural connection

 

4.6 Use (+) to realize the connection operation:

1. No (+) connection:

 

 

2. Left (+) connection: (equivalent to Right Join)

 

 

3. Right (+) connection: (equivalent to Left Join)

 

Note: You cannot add (+) to both left and right.

4.7 Self Join and Cartesian Products

          

                   Self-connected Cartesian product

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326069090&siteId=291194637