Oracle multi-table joins three algorithms

Three ways to multi-table joins Detailed hash join, merge join, nested loop
when the joint multi-table queries, if we look at its implementation plan, which you will find how connections between tables. The connection between multiple tables in three ways: Nested Loops, Hash Join Sort Merge Join and what type of connection specific application depends on the current optimization mode (ALL_ROWS and RULE)..

One. HASH JOIN: Hash Join

Hash join hash join common way is bigger CBO data sets when connected, the optimizer uses two tables smaller table (typically smaller that table or data source) using linkages (JOIN KEY) in memory up the hash table, the column data stored in the hash list, and the larger the scan table, hash table after the detection of the same for the hASH JOIN KEY, find the row with the matching hash table. Note that: If the HASH table is too large a structure in memory, it is divided into several partition, temporary segment written to disk, it will write one more cost, reduces efficiency.

This approach applies to the case of smaller tables can put in the memory, so the total cost is the cost of the visit and two tables. However, in the case of very large tables not entirely into memory, it will be time optimizer into several different partitions can not be put into the part of the memory segment written to a temporary disk partition, in which case there must be large temporary segment thereby maximize I / O performance.

Can USE_HASH (table_name1 table_name2) hint to force using a hash join.

Usage:

Hash join the amount of data in two tables of very different times.

clip_image001

two. SORT MERGE JOIN: sort-merge joins

Merge Join is the first association table associated with each column make sorting and extracting data from the respective sorting table, to another sort table do match.

Because merge join need to do more to sort, so consume more resources. Generally, the place can use merge join, hash join can play better performance, that is connected hash effects than sort-merge joins better. However, if the line source has been over-discharge sequence, when the connection does not need to perform a sort-merge sorted, then sort-merge join performs better than hash join.

It may be used USE_MERGE (table_name1 table_name2) to force the use of sort-merge join.

Applicable:

1.RBO mode

2. inequivalent associated (>, <,> =, <=, <>)

3.HASH_JOIN_ENABLED=false

4. no index, and the case where the data has been sorted.

clip_image002

three. NESTED LOOP: nested loop join

Nested loops work cycle is read from a table data (table driven outer table), then access another table (look-up table inner table, usually index). Each record corresponding to the inner table-driven row in Table JOIN. Similarly a nested loop.

For data sub-set is connected is small, nested loop join is a better choice. In nested loops, inner driven appearance, the appearance of the inner table for each row to be returned to retrieve it and find the line matching, so the whole query returns a result set is not too large (greater than 10,000 is not suitable), to smaller subset of the return as the outer table (CBO outer drive default table), and connecting the inner field table must be indexed. Of course, you can also use ORDERED prompted to change the default drive CBO table.

Use USE_NL (table_name1 table_name2) but forced CBO perform nested loop join.

Applicable:

Adapted to drive the recording sheet set small (<10,000) and the inner table requires a valid access method (Index), and when the index good selectivity.

JOIN order is important, driving record set the table must be small, returns a result set response time is the fastest.

Reprinted from: https://www.cnblogs.com/xqzt/p/4469673.html

Published 25 original articles · won praise 1 · views 10000 +

Guess you like

Origin blog.csdn.net/kimi_Christmas/article/details/89184827