Range (non-equi) join: NL vs sort merge
A join connected by BETWEEN, such as an amount band table, cannot use a hash join. Solve the problem of an NL join reading a large table once per band by using a sort merge join.
SQL
Situation
A report counts 2025 orders by amount band (S, M, L, XL). The band definitions live in a small code table, and each order is attached to a band with amount BETWEEN lower AND upper. There are only 4 bands, yet the report is slow.
Tables
| Table | Column | Type | Description |
|---|---|---|---|
ORDERS | ORD_DT | DATE | Order date and time (2023-01-01 to 2025-09-26, 2,000 per day) |
ORDERS | AMT | NUMBER | Order amount (evenly distributed from 1,000 to 500,999) |
ORDERS (2 million rows) has no index starting with ORD_DT or AMT. The band table is 4 rows built with a WITH clause for the exercise.
Query
BEFORE · NL join (repeated per band)
WITH band AS (
SELECT 'S' AS band, 0 AS lo, 99999 AS hi FROM dual UNION ALL
SELECT 'M', 100000, 249999 FROM dual UNION ALL
SELECT 'L', 250000, 399999 FROM dual UNION ALL
SELECT 'XL', 400000, 9999999 FROM dual
)
SELECT /*+ LEADING(b o) USE_NL(o) FULL(o) */ b.band, COUNT(*) AS cnt, SUM(o.amt) AS amt
FROM band b
JOIN orders o ON o.amt BETWEEN b.lo AND b.hi
WHERE o.ord_dt >= DATE '2025-01-01'
GROUP BY b.band
Run it and look at Starts and Buffers of the ORDERS FULL SCAN.
Approach
1. See how many times the large table is read
If the inner side of an NL join is TABLE ACCESS FULL, the whole table is read again for each driving row. Check whether Starts equals the number of bands (4).
2. Check whether the join condition is an equality
A hash join finds matches by the hash value of the join key, so it needs an = condition. A join connected only by BETWEEN, <, or > cannot use a hash join. Even a USE_HASH hint is ignored.
3. If no index can be built on the inner side
Even with an AMT index, looking up the 538,000 orders of 2025 one by one through the index costs more than a FULL SCAN. Without changing the structure, find a join method that reads the large table only once.
4. Think of matching by sorted order
If both sides are sorted by the join key (amount), walking down the band table attaches the rows in each range in a single pass. Compare the sort cost with the cost of repeated scans.
Explanation
NL join → sort merge join
AFTER · Sort merge join
WITH band AS (
SELECT 'S' AS band, 0 AS lo, 99999 AS hi FROM dual UNION ALL
SELECT 'M', 100000, 249999 FROM dual UNION ALL
SELECT 'L', 250000, 399999 FROM dual UNION ALL
SELECT 'XL', 400000, 9999999 FROM dual
)
SELECT /*+ LEADING(b o) USE_MERGE(o) FULL(o) */ b.band, COUNT(*) AS cnt, SUM(o.amt) AS amt
FROM band b
JOIN orders o ON o.amt BETWEEN b.lo AND b.hi
WHERE o.ord_dt >= DATE '2025-01-01'
GROUP BY b.band
Why it got faster
- In Before, the
ORDERSFULL SCAN has Starts of 4 and about 55,900 Buffers. It read the 2-million-row table from start to end once per band. - After reads
ORDERSonly once (about 14,000 Buffers), sorts the 538,000 orders of 2025 by amount withSORT JOIN, and then matches them against the band table. MERGE JOINis the only "read once" join method that can handle a range condition. Even without an equality condition, it uses the sorted order to attach rows from the start of the range to its end.
Why this is optimal
- Reading
ORDERSonce is the minimum this report needs (it must count every 2025 order, and there is noORD_DTindex). - The remaining cost is sorting 538,000 rows. Check whether the sort finishes in memory (PGA) with Used-Mem and Used-Tmp in the execution plan. If the sort spills to TEMP, that part is the next tuning target.
- To go further, replace the band calculation with a
CASEexpression instead of a join. If the band table rarely changes,GROUP BY CASE ...gives the same result without a join (and without a sort).
Variation: forcing a hash join
TRY · USE_HASH (ignored)
WITH band AS (
SELECT 'S' AS band, 0 AS lo, 99999 AS hi FROM dual UNION ALL
SELECT 'M', 100000, 249999 FROM dual UNION ALL
SELECT 'L', 250000, 399999 FROM dual UNION ALL
SELECT 'XL', 400000, 9999999 FROM dual
)
SELECT /*+ LEADING(b o) USE_HASH(o) */ b.band, COUNT(*) AS cnt, SUM(o.amt) AS amt
FROM band b
JOIN orders o ON o.amt BETWEEN b.lo AND b.hi
WHERE o.ord_dt >= DATE '2025-01-01'
GROUP BY b.band
- The execution plan is an NL join again. The Hint Report in the Raw tab shows
USE_HASHas not used (Unused). Without an equality condition, the hash join drops out of the candidates.
Variation: band calculation without a join
TRY · Band calculation with a CASE expression
SELECT CASE WHEN amt <= 99999 THEN 'S' WHEN amt <= 249999 THEN 'M' WHEN amt <= 399999 THEN 'L' ELSE 'XL' END AS band,
COUNT(*) AS cnt, SUM(amt) AS amt
FROM orders
WHERE ord_dt >= DATE '2025-01-01'
GROUP BY CASE WHEN amt <= 99999 THEN 'S' WHEN amt <= 249999 THEN 'M' WHEN amt <= 399999 THEN 'L' ELSE 'XL' END
- Buffers are the same as After, and
SORT JOINdisappears. If the band table must be managed as data, use the join; if it is fixed,CASEis better.
Key points
- A hash join requires an equality condition. For a join with only range conditions, only the NL join and the sort merge join remain.
- If the inner side of an NL join is a FULL SCAN, the cost is
driving rows × table size. Even a few driving rows add up quickly on a large table. - A sort merge join pays a sort cost and removes repeated scans. If an input is already sorted (index order), the sort is skipped too.
- Differences by DB: PostgreSQL's Merge Join works only with equality conditions, so a range join is processed as an NL join. That is why this scenario is Oracle-only. Other DBs use a
CASEexpression or range types (int4range, GiST index). - This continues the join method comparison table in scenario 6.