1. Gyanastra
  2. Database Systems
  3. Join Algorithms Extended: Indexed Nested Loop, Hash Join and Output Size Estimation
  4. The Cost Model: What We Are Counting
Free lesson, no account needed 4 min read

Database Systems · Join Algorithms Extended: Indexed Nested Loop, Hash Join and Output Size Estimation

The Cost Model: What We Are Counting

This chapter continues the join-cost discussion. It assumes you already know what a join is (Relational Algebra chapter) and that you have seen the basic nested-loop and sort-merge ideas. Here we go deeper: the exact cost model GATE uses, indexed nested loop join, hash join (including recursive partitioning), sort-merge revisited, block nested loop with MM buffers, and — very important for GATE — output size estimation (how many tuples and blocks a join produces).


1.1 The symbols (memorise these)

SymbolMeaning
brb_rnumber of blocks (disk pages) holding relation rr
nrn_rnumber of tuples (records) in relation rr
bsb_s, nsn_ssame for relation ss
MMnumber of buffer blocks available in main memory
V(A,r)V(A,r)number of distinct values of attribute AA in relation rr
frf_rblocking factor of rr = tuples per block = nr/brn_r / b_r (so br=⌈nr/fr⌉b_r = \lceil n_r / f_r\rceil)

Two relations: rr (outer) and ss (inner). The join condition is written r⋈sr \bowtie s.

1.2 Two models — state which one you are using

Model A — "block accesses" (the simple model, used by most GATE questions). Every block read or written costs 1. We ignore seeks entirely. Cost = number of block transfers. This is the model in most GATE numerical questions unless the question explicitly mentions seeks.

Model B — seek + transfer (Silberschatz). Cost = b×tT+S×tSb \times t_T + S \times t_S, where tTt_T = time to transfer one block, tSt_S = time for one seek, SS = number of seeks. When a question gives tSt_S and tTt_T you must count seeks separately. When it does not, use Model A.

Convention note. Elmasri–Navathe count block transfers and treat a "block access" as one unit; Silberschatz writes br+bsb_r + b_s transfers plus seeks. GATE almost always uses Model A. If a question says "assume each block access costs 1" or gives no seek time, it is Model A.

1.3 The running example (used throughout)

r : b_r = 1000 blocks,  n_r = 10000 tuples   (so f_r = 10 tuples/block)
s : b_s =  500 blocks,  n_s =  5000 tuples   (so f_s = 10 tuples/block)
M = 52 buffer blocks
Join attribute A, with V(A,r) = 1000, V(A,s) = 500

Check: fr=10000/1000=10f_r = 10000/1000 = 10 ✓, fs=5000/500=10f_s = 5000/500 = 10 ✓.


Plain (Tuple-level) Nested Loop Join

For each tuple of rr, scan all blocks of ss:

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

  • nr⋅bsn_r \cdot b_s: for each of the nrn_r tuples of rr, we read all bsb_s blocks of ss.
  • +br+ b_r: read rr once.

On the example: 10000×500+1000=5,000,000+1000=5,001,00010000 \times 500 + 1000 = 5{,}000{,}000 + 1000 = 5{,}001{,}000 block accesses. Terrible.

Special case — smaller relation fits in memory. If the smaller relation fits in M−2M-2 buffer blocks (2 blocks reserved for input/output), cost is just

br+bsb_r + b_s

because you load the small relation once and stream the other. On the example, M−2=50M-2 = 50; bs=500>50b_s = 500 > 50, so this does not apply here.


Block Nested Loop Join (BNLJ) with MM buffers

Use M−2M-2 blocks to hold a chunk of the outer relation; use 1 block for the inner scan and 1 for output.

cost=br+⌈brM−2⌉⋅bs\text{cost} = b_r + \left\lceil \frac{b_r}{M-2}\right\rceil \cdot b_s

  • brb_r: read the outer relation once.
  • ⌈br/(M−2)⌉\lceil b_r/(M-2)\rceil: number of chunks the outer is split into.
  • ×bs\times b_s: for each chunk, rescan the whole inner relation.

Which relation should be outer? The one that minimises the cost. Put the smaller relation as outer (fewer chunks ⇒ fewer inner rescans). But always compute both.

Worked on the example (M−2=50M-2 = 50):

  • rr outer: 1000+⌈1000/50⌉⋅500=1000+20⋅500=1000+10000=11,0001000 + \lceil 1000/50\rceil \cdot 500 = 1000 + 20 \cdot 500 = 1000 + 10000 = 11{,}000.
  • ss outer: 500+⌈500/50⌉⋅1000=500+10⋅1000=500+10000=10,500500 + \lceil 500/50\rceil \cdot 1000 = 500 + 10 \cdot 1000 = 500 + 10000 = 10{,}500.

Best BNLJ = 10,500 (with ss as outer). This is the number to remember for the running example.


Pages 1–2 of 2