4.4.13 · D3Databases

Worked examples — Indexing — B-tree index, hash index, full-text

3,479 words16 min readBack to topic

Before we start, five words we will use a hundred times, defined from zero:


The scenario matrix

Every query this topic can throw at you falls into one of these cells. Each worked example below is tagged with the cell it covers.

# Cell (scenario class) Best index Covered by
C1 Exact equality = Hash or B-tree Ex 1
C2 Range <, >, BETWEEN B-tree only Ex 2
C3 Sorting ORDER BY B-tree only Ex 3
C4 Prefix LIKE 'app%' B-tree Ex 4
C5 Inside-word LIKE '%pp%' (degenerate for B-tree) Full-text Ex 5
C6 Composite (a,b) + leftmost-prefix trap B-tree Ex 6
C7 Zero / empty result (degenerate input) any Ex 7
C8 Limiting behaviour: huge, hash collisions analysis Ex 8
C9 Real-world word problem (choose the index) reasoning Ex 9
C10 Exam twist: " beats so hash wins" steel-man Ex 10

The columns "sign/quadrant" of a trig problem become here "query shape" (equality / range / order / substring) and "input degeneracy" (empty table, empty result, collisions, huge ). We cover them all.












Recall Scenario checklist — can you place each in a matrix cell?

Query is col = value and nothing else ::: Cell C1 — hash () or B-tree. Query is BETWEEN / < / > ::: Cell C2 — B-tree only (hash can't walk ranges). Query is ORDER BY col ::: Cell C3 — B-tree gives sorted leaves free. Query is LIKE 'pre%' ::: Cell C4 — B-tree via known-prefix range. Query is LIKE '%mid%' ::: Cell C5 — B-tree degenerates to scan; use full-text. Query filters b alone on index (a,b) ::: Cell C6 — leftmost-prefix fails; scan. Query returns nothing / table empty ::: Cell C7 — index still just , no scan. explodes or buckets overflow ::: Cell C8 — B-tree height ; hash degrades to comparisons. Query needs only indexed columns ::: index-only scan, no table fetches; otherwise add one fetch per matching row.