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.
| Table | Column | Type | Description |
|---|---|---|---|
CUSTOMERS | CUST_ID | NUMBER | Customer ID (PK) |
CUSTOMERS | CUST_NM | VARCHAR2 | Customer name |
CUSTOMERS | GRADE | VARCHAR2 | VIP 1%, GOLD 9%, NORMAL 90% |
ORDERS | CUST_ID | NUMBER | Ordering customer ID |
ORDERS | ORD_DT | DATE | Order date and time |
ORDERS | AMT | NUMBER | Order amount |
| Index | Columns |
|---|---|
CUSTOMERS_PK | CUST_ID |
CUSTOMERS_X1 | GRADE |
ORDERS_X1 | CUST_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
ORDERShas after applyingORD_DT >= 2025-06-01 - How many rows
CUSTOMERShas after applyingGRADE = '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
ORDERSis 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
ORDERSwith a FULL SCAN, then probesCUSTOMERS_PKthat many times. The inner operation of the NL join has 236,000 Starts. - After reads the 1,000 VIP customers and probes
ORDERS_X1withCUST_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 ofORDERS_X1as 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).AMTis not in the index, so this is unavoidable unless the index changes. - Finding the 1,000 VIP customers also goes through
CUSTOMERS_X1and 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
ORDERSis 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_X1isCUST_ID. LEADING(a b)sets only the order. The join method is specified separately withUSE_NLorUSE_HASH.ORDEREDis the older way that follows the FROM clause order.