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 buffers, and — very important for GATE — output size estimation (how many tuples and blocks a join produces).
1.1 The symbols (memorise these)
| Symbol | Meaning |
|---|---|
| number of blocks (disk pages) holding relation | |
| number of tuples (records) in relation | |
| , | same for relation |
| number of buffer blocks available in main memory | |
| number of distinct values of attribute in relation | |
| blocking factor of = tuples per block = (so ) |
Two relations: (outer) and (inner). The join condition is written .
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 = , where = time to transfer one block, = time for one seek, = number of seeks. When a question gives and 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 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: ✓, ✓.
Plain (Tuple-level) Nested Loop Join
For each tuple of , scan all blocks of :
- : for each of the tuples of , we read all blocks of .
- : read once.
On the example: block accesses. Terrible.
Special case — smaller relation fits in memory. If the smaller relation fits in buffer blocks (2 blocks reserved for input/output), cost is just
because you load the small relation once and stream the other. On the example, ; , so this does not apply here.
Block Nested Loop Join (BNLJ) with buffers
Use blocks to hold a chunk of the outer relation; use 1 block for the inner scan and 1 for output.
- : read the outer relation once.
- : number of chunks the outer is split into.
- : 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 ():
- outer: .
- outer: .
Best BNLJ = 10,500 (with as outer). This is the number to remember for the running example.