Driving set and join order

An NL join repeats as many times as the row count of the side read first. OPTION (FORCE ORDER) compares the two orders directly.

SQL

Situation

A list of VIP customers' orders from June on. The query is written in the order FROM orders o JOIN customers c, and OPTION (FORCE ORDER), meaning "join in the written order", was attached earlier together with an NL join hint. The result is 2,376 rows, but it takes several seconds.

Tables

TableColumnDescription
customerscust_id, cust_nm, grade100,000 customers, 1,000 VIP
orderscust_id, ord_dt, amt2,000,000 orders, about 230,000 from June on
IndexColumns
customers_pk (clustered)cust_id
customers_x1grade
orders_pk (clustered)ord_id
orders_x1cust_id, ord_dt

Query

BEFORE · Orders first (FORCE ORDER)

SELECT c.cust_nm, o.ord_dt, o.amt
FROM orders o
INNER LOOP JOIN customers c ON c.cust_id = o.cust_id
WHERE c.grade = 'VIP'
  AND o.ord_dt >= CAST('2025-06-01' AS datetime2(0))
OPTION (FORCE ORDER)

Run it and check the Starts of the inner operator.

Approach

1. Check the row count of the outer (driving) set

An NL join searches the inner side once per outer row. If the outer side is the 230,000 orders from June on, the inner side is searched 230,000 times.

2. Check which side's condition reduces more

grade = 'VIP' reduces to 1,000 customers, and ord_dt >= June reduces to 230,000 orders. Starting from the smaller side (1,000 VIPs) cuts the repetitions to 1,000.

3. Check whether the inner side has a matching index

Starting from customers, the inner side is "that customer's orders from June on". orders_x1(cust_id, ord_dt) seeks exactly that range.

Explanation

Customers first (1,000 VIPs → orders)

AFTER · Customers first (FORCE ORDER)

SELECT c.cust_nm, o.ord_dt, o.amt
FROM customers c
INNER LOOP JOIN orders o WITH (INDEX(orders_x1)) ON o.cust_id = c.cust_id
WHERE c.grade = 'VIP'
  AND o.ord_dt >= CAST('2025-06-01' AS datetime2(0))
OPTION (FORCE ORDER)

Why it got faster

  • Before puts the 236,000 orders from June on as the outer side and seeks customers_x1 per row (Starts 236,000). Buffers about 475,000.
  • After puts the 1,000 VIPs as the outer side and seeks orders_x1 for the (cust_id, from June on) range per customer (Starts 1,000). The orders' amt is fetched with Key Lookups (2,376 times). Buffers about 10,400.

Why this is optimal

  • The repetitions drop to the number of VIP customers (1,000) and the result row count (2,376). Most of the remaining cost is the Key Lookup per result row (about 6,800), because amt is not in orders_x1.

Variation: removing all hints

TRY · No hints

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 >= CAST('2025-06-01' AS datetime2(0))
  • Regardless of the written order, the optimizer picks the same plan as After, reading customers first (Buffers about 10,400). The problem in Before is that FORCE ORDER enforced the order written in the FROM clause as is.

Key points

  • The cost of an NL join is outer row count × inner search cost. Put the small set on the outer side.
  • OPTION (FORCE ORDER) uses the FROM clause order as the join order as is. When the query is rewritten or the data changes, a bad order can get locked in.
  • DB differences: Oracle enforces the same with LEADING(c o), and PostgreSQL with join_collapse_limit = 1 and the written order.