Driving set and join order

An NL join repeats once per row of the side read first. Compare the two orders directly with the LEADING hint.

SQL

Situation

A VIP customer management screen retrieves VIP customers' orders from June 2025 onward. The query connects the two tables with an NL join, and fetching all results takes a long time.

Tables

CUSTOMERS (100,000 rows) holds customers, and ORDERS (2 million rows) holds orders accumulated at 2,000 per day.

TableColumnTypeDescription
CUSTOMERSCUST_IDNUMBERCustomer ID (PK)
CUSTOMERSCUST_NMVARCHAR2Customer name
CUSTOMERSGRADEVARCHAR2VIP 1%, GOLD 9%, NORMAL 90%
ORDERSCUST_IDNUMBEROrdering customer ID
ORDERSORD_DTDATEOrder date and time
ORDERSAMTNUMBEROrder amount
IndexColumns
CUSTOMERS_PKCUST_ID
CUSTOMERS_X1GRADE
ORDERS_X1CUST_ID, ORD_DT

There is no index starting with ORD_DT.

Query

Fetch the orders of VIP customers (1,000) since 2025-06-01 together with the customer name. Across all customers, there are about 236,000 orders since 2025-06-01.

BEFORE · LEADING(o c)

SELECT /*+ LEADING(o c) USE_NL(c) */
       c.cust_nm, o.ord_dt, o.amt
FROM orders o
JOIN customers c ON c.cust_id = o.cust_id
WHERE c.grade = 'VIP'
  AND o.ord_dt >= DATE '2025-06-01'

Run it and check in the execution plan which table is read first and the Starts and Buffers of the operation on the inner side of the NL join.

Approach

1. See which side is the driving set

An NL join probes the inner table once for each row of the side read first (the driving set). In the execution plan, the first child directly under NESTED LOOPS is the driving set. Check whether its A-Rows equals the Starts of the inner operation.

2. Ask whether the repeat count fits the result

If the inner operation's Starts is in the hundreds of thousands, the probe was repeated that many times. Yet the final result is only the VIP customers' orders. Most probes go to non-VIP customers and are thrown away.

3. Compare both sides by row count after applying the conditions

Look at the row count after applying the conditions, not the table size.

  • How many rows ORDERS has after applying ORD_DT >= 2025-06-01
  • How many rows CUSTOMERS has after applying GRADE = 'VIP'

Putting the side with fewer rows first reduces the repeat count.

4. Check that the inner table can be probed with a join column index

Changing the order makes ORDERS the inner side. For an NL join to be efficient, the inner table needs an index that starts with the join column. See which columns ORDERS_X1 has and whether the date condition can also be used in the search range.

Switching the join method to a hash join is another direction. But VIP orders are a fraction of the total and the inner side can be probed narrowly with an index, so in this scenario fixing the order comes first. As long as ORDERS is read with a FULL SCAN, the cost of reading 2 million rows remains.

Explanation

Making VIP customers the driving set

AFTER · LEADING(c o)

SELECT /*+ LEADING(c o) USE_NL(o) INDEX(o orders_x1) */
       c.cust_nm, o.ord_dt, o.amt
FROM customers c
JOIN orders o ON o.cust_id = c.cust_id
WHERE c.grade = 'VIP'
  AND o.ord_dt >= DATE '2025-06-01'

Why it got faster

  • Before filters 236,000 rows out of ORDERS with a FULL SCAN, then probes CUSTOMERS_PK that many times. The inner operation of the NL join has 236,000 Starts.
  • After reads the 1,000 VIP customers and probes ORDERS_X1 with CUST_ID = ? AND ORD_DT >= ? for each customer.
  • Starts drops to 1,000, and Buffers drops sharply too.

Why this is optimal

  • The driving set is the smallest, 1,000 VIP customers, and the inner probe uses both (CUST_ID, ORD_DT) columns of ORDERS_X1 as access conditions. Only the needed segment is read for each customer.
  • The remaining cost is visiting the table once per order found (TABLE ACCESS BY INDEX ROWID). AMT is not in the index, so this is unavoidable unless the index changes.
  • Finding the 1,000 VIP customers also goes through CUSTOMERS_X1 and then the table. The customer name (CUST_NM) is needed, so this cost remains too.

Comparison lab

Remove the hints and run it to see which side the optimizer chooses.

TRY · No hint

SELECT c.cust_nm, o.ord_dt, o.amt
FROM orders o
JOIN customers c ON c.cust_id = o.cust_id
WHERE c.grade = 'VIP'
  AND o.ord_dt >= DATE '2025-06-01'

Key points

  • The cost of an NL join is driving set rows × cost of one inner probe. The side with fewer rows after filtering must come first.
  • The criterion is the row count after applying the conditions, not the table size. Even though ORDERS is bigger, it can be the driving table if its condition is very selective.
  • An NL join works only if the inner table has an index starting with the join column. That is why the leading column of ORDERS_X1 is CUST_ID.
  • LEADING(a b) sets only the order. The join method is specified separately with USE_NL or USE_HASH. ORDERED is the older way that follows the FROM clause order.