Sub topic 6
Recognizing Join Methods
Assume you know basic SELECT statements, joins, indexes, and primary keys. You should also know how to generate a plan with EXPLAIN PLAN and read a plan tree from the bottom upward.
A join method is the algorithm Oracle uses to combine rows from two row sources. The optimizer chooses a method using estimated cardinalities, available indexes, join conditions, memory, and cost. The three common methods are nested loops, hash joins, and sort merge joins.
Nested loops joins
A nested loops join processes one row source as the outer input. For each outer row, Oracle finds matching rows in the inner input.
EXPLAIN PLAN FOR
SELECT e.last_name, d.department_name
FROM hr.employees e
JOIN hr.departments d
ON d.department_id = e.department_id
WHERE e.employee_id < 110;
SELECT *
FROM TABLE(DBMS_XPLAN.DISPLAY(format => 'TYPICAL'));A typical plan might show an index lookup on DEPARTMENTS under a nested loops operation. Oracle reads qualifying employees, then uses each employee's department_id to find the matching department.
Nested loops work well when:
- The outer input returns few rows.
- The inner input has a selective index on the join column.
- The query returns only a small result.
They can perform badly when the outer input returns many rows. An inner index lookup repeated thousands of times can create many buffer gets. An index does not automatically make nested loops efficient.
Hash joins
A hash join builds an in-memory hash table from one input, called the build input. It then scans the other input, called the probe input, and looks for matching hash values.
Hash joins are often effective when:
- Both inputs contain many rows.
- The join uses equality, such as
a.id = b.id. - Full scans are cheaper than repeated index lookups.
- The required hash table fits in memory.
If the hash table does not fit in memory, Oracle can write temporary hash partitions to disk. This increases I/O and can make the join much slower.
Sort merge joins
A sort merge join sorts both inputs by the join key and then walks through the sorted results to find matches. It can join some conditions that are not simple equality conditions, although hash joins are usually preferred for equality joins.
Sort merge joins may be useful when:
- The inputs are already sorted.
- The join condition is a range condition.
- Sorting is needed for another operation anyway.
The sorting cost can make this method expensive. A plan showing SORT JOIN or SORT ORDER BY does not necessarily mean the join method is sort merge. Read the parent operation, which is normally named MERGE JOIN.
Join order and join method are different
The join order says which tables Oracle combines first. The join method says how it combines them. A plan can join EMPLOYEES to DEPARTMENTS with nested loops, then use a hash join to combine that result with another table.
Read the children beneath a join operation to identify its inputs. In most plans, the operation on the right or lower part of the tree supplies the first input, but the exact runtime behavior depends on the operation and plan structure. Use the plan's indentation and Id and Parent Id relationships rather than guessing from SQL table order.
A practical reading approach
When you see a join in a plan:
- Identify the join method.
- Identify the two child row sources.
- Check which child is likely to be the outer or build input.
- Compare
E-Rowswith actual rows when runtime statistics are available. - Check whether the method causes repeated index probes, large scans, or temporary spills.
Exercise
Exercise
A nested loops join has an outer input with 500,000 actual rows, and the inner table is accessed through an index for every outer row. What should you investigate first?
Show answer
Investigate whether the outer input is too large for nested loops and whether a hash join or a more selective predicate would reduce repeated inner lookups. Confirm the diagnosis with Starts and Buffers in DBMSXPLAN.DISPLAYCURSOR.