Outer join direction and HASH JOIN RIGHT OUTER

It is easy to assume an outer join must read the preserved table first. Instead of an NL join driven by a large preserved table, switch the build side of a hash join (SWAP_JOIN_INPUTS) and load the smaller side into memory.

SQL

Situation

A screen shows "customers with orders this month and the order count" by region. Customers with no orders must not drop out of the aggregation, so it was written as CUSTOMERS LEFT JOIN ORDERS. As the number of customers grew, the screen became slow.

Tables

TableColumnTypeDescription
CUSTOMERSCUST_IDNUMBERCustomer ID (PK)
CUSTOMERSREGIONVARCHAR25 regions
ORDERSCUST_IDNUMBERID of the ordering customer
ORDERSORD_DTDATEOrder date and time (last date 2025-09-26)
IndexColumns
CUSTOMERS_PKCUST_ID
ORDERS_X1CUST_ID, ORD_DT

CUSTOMERS has 100,000 rows, and there are 52,000 orders since 2025-09-01.

Query

BEFORE · NL outer join

SELECT /*+ LEADING(c o) USE_NL(o) */ c.region, COUNT(o.ord_id) AS cnt
FROM customers c
LEFT JOIN orders o ON o.cust_id = c.cust_id AND o.ord_dt >= DATE '2025-09-01'
GROUP BY c.region

Run it and look at Starts and Buffers of the ORDERS_X1 lookup.

Approach

1. See how many inner lookups happen

The preserved table CUSTOMERS has 100,000 rows, so the NL join probes ORDERS_X1 100,000 times. Only some customers have orders this month, yet it visits the index each time to confirm "none" for customers without orders too.

2. Ask whether the order is really fixed for an outer join

In an NL join, the preserved table (the left side of LEFT JOIN) must be the outer (driving) side. Starting from the inner table gives no way to find preserved rows without a match. A hash join has no such restriction.

3. Decide which side goes into memory

A hash join builds a hash table from one side (Build) and probes it with the other (Probe). Work out which set is smaller after the conditions are applied. The preserved table does not have to be the Build side.

Explanation

NL outer join → HASH JOIN RIGHT OUTER

AFTER · HASH JOIN RIGHT OUTER

SELECT /*+ LEADING(c o) USE_HASH(o) SWAP_JOIN_INPUTS(o) */ c.region, COUNT(o.ord_id) AS cnt
FROM customers c
LEFT JOIN orders o ON o.cust_id = c.cust_id AND o.ord_dt >= DATE '2025-09-01'
GROUP BY c.region

Why it got faster

  • In Before, the ORDERS_X1 INDEX RANGE SCAN has Starts of 100,000, and total Buffers are about 119,300.
  • After reads each table only once, with about 14,500 Buffers. The execution plan shows HASH JOIN RIGHT OUTER. It builds the hash table from this month's orders (the inner side of the outer join) and probes it with the 100,000 customers. Customers without a match stay in the result because the Probe side is the preserved table.
  • SWAP_JOIN_INPUTS(o) means "put o on the Build side". "RIGHT OUTER" is the join direction seen from the Build input, so the outer join meaning is unchanged.

Why this is optimal

  • The repeated lookups are gone, and reading each of the two tables once is the minimum cost for this aggregation.
  • Most of the remaining cost is the ORDERS FULL SCAN (about 14,000). Only this month's orders are needed, but there is no index starting with ORD_DT.
  • The Build side (52,000 orders this month) is smaller than the 100,000 customers, so the hash table is small too. Check it with Used-Mem.

Variation: only reversing the driving order

TRY · LEADING(o c) USE_NL(c)

SELECT /*+ LEADING(o c) USE_NL(c) */ c.region, COUNT(o.ord_id) AS cnt
FROM customers c
LEFT JOIN orders o ON o.cust_id = c.cust_id AND o.ord_dt >= DATE '2025-09-01'
GROUP BY c.region
  • An NL outer join starting from ORDERS is impossible, so the optimizer cannot follow this hint. The execution plan changes to a hash join (HASH JOIN OUTER). Check the ignored hint in the Hint Report.

Key points

  • In an NL outer join, the preserved table is always the driving table. If the preserved table is large, the number of lookups is just as large.
  • A hash outer join can choose its Build side. Put the smaller side (after conditions) on Build, and if the preserved table is large, send the inner table to Build with SWAP_JOIN_INPUTS.
  • PostgreSQL (Hash Right Join) and SQL Server (Hash Match (Right Outer Join)) switch the Build side the same way.
  • This continues scenario 6 (choosing the Build input) and scenario 5 (driving order).