Database Systems · Join Algorithms Extended: Indexed Nested Loop, Hash Join and Output Size Estimation
Indexed Nested Loop Join (INLJ)
If the inner relation has an index on the join attribute, we do not scan all of for each tuple of ; we do one index lookup.
- : read the outer relation once.
- : one lookup per tuple of .
- : cost of one index lookup on (in block accesses).
4.1 What is ?
For a B⁺-tree index of height (root to leaf), a lookup costs block reads to reach the leaf, plus the data block(s):
- Clustered (primary) index — matching tuples are stored together, so usually 1 data block:
- Unclustered (secondary) index — matching tuples may be scattered, so up to one block per matching tuple. If the join attribute is a key of , at most 1 tuple matches, so still . If not a key, can be much larger (≈ number of matching tuples).
Convention note. Some texts write (counting only index blocks) and add data separately; others write . GATE usually gives you and expects for a clustered/key index. Read the question: if it says "index lookup costs block accesses", use that directly.
4.2 When does INLJ beat BNLJ?
INLJ wins when (the inner-rescan term of BNLJ). Intuitively: a cheap index lookup ( small) beats rescanning a big inner relation.
4.3 Worked example
Suppose has a clustered B⁺-tree index on of height . Then .
- outer, index on : .
- outer, index on (assume same , ): .
Best INLJ = 20,500 (with as outer). Compare with BNLJ's 10,500 — here BNLJ is better because is large and is not small enough. If were 1 (say a hash index with 1 block), INLJ would give , beating BNLJ.
Trap. INLJ is not automatically the winner. It wins only when is small. Always plug in numbers.