1. Gyanastra
  2. Database Systems
  3. Join Algorithms Extended: Indexed Nested Loop, Hash Join and Output Size Estimation
  4. Indexed Nested Loop Join (INLJ)
Free lesson, no account needed 4 min read

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 ss for each tuple of rr; we do one index lookup.

cost=br+nr⋅c\text{cost} = b_r + n_r \cdot c

  • brb_r: read the outer relation once.
  • nrn_r: one lookup per tuple of rr.
  • cc: cost of one index lookup on ss (in block accesses).

4.1 What is cc?

For a B⁺-tree index of height hh (root to leaf), a lookup costs hh block reads to reach the leaf, plus the data block(s):

  • Clustered (primary) index — matching tuples are stored together, so usually 1 data block: c=h+1c = h + 1
  • Unclustered (secondary) index — matching tuples may be scattered, so up to one block per matching tuple. If the join attribute is a key of ss, at most 1 tuple matches, so still c=h+1c = h + 1. If not a key, cc can be much larger (≈ number of matching tuples).

Convention note. Some texts write c=hc = h (counting only index blocks) and add data separately; others write h+1h+1. GATE usually gives you hh and expects c=h+1c = h+1 for a clustered/key index. Read the question: if it says "index lookup costs cc block accesses", use that cc directly.

4.2 When does INLJ beat BNLJ?

INLJ wins when nr⋅c<⌈br/(M−2)⌉⋅bsn_r \cdot c < \lceil b_r/(M-2)\rceil \cdot b_s (the inner-rescan term of BNLJ). Intuitively: a cheap index lookup (cc small) beats rescanning a big inner relation.

4.3 Worked example

Suppose ss has a clustered B⁺-tree index on AA of height h=3h = 3. Then c=3+1=4c = 3 + 1 = 4.

  • rr outer, index on ss: br+nr⋅c=1000+10000⋅4=1000+40000=41,000b_r + n_r \cdot c = 1000 + 10000 \cdot 4 = 1000 + 40000 = 41{,}000.
  • ss outer, index on rr (assume same h=3h=3, c=4c=4): bs+ns⋅c=500+5000⋅4=500+20000=20,500b_s + n_s \cdot c = 500 + 5000 \cdot 4 = 500 + 20000 = 20{,}500.

Best INLJ = 20,500 (with ss as outer). Compare with BNLJ's 10,500 — here BNLJ is better because nrn_r is large and c=4c=4 is not small enough. If cc were 1 (say a hash index with 1 block), INLJ would give 500+5000=5500500 + 5000 = 5500, beating BNLJ.

Trap. INLJ is not automatically the winner. It wins only when nouter⋅cn_{\text{outer}} \cdot c is small. Always plug in numbers.


Pages 1–2 of 2