4.4.13 · D1Databases

Foundations — Indexing — B-tree index, hash index, full-text

2,262 words10 min readBack to topic

Before you can read the parent note, you need to own every symbol and word it throws at you. This page builds each one from nothing, in the order they depend on each other. Nothing here is assumed — if the parent used it, we define it.


0. The stage: rows, tables, and disk

Picture a shoebox stuffed with index cards, one card per person, in the order they walked in the door. That shoebox is the heap. There is no rule about where any card sits.

Figure — Indexing — B-tree index, hash index, full-text

Look at the figure. To find the card for "email = zara@x.com" you have no shortcut — you pull card 1, check, pull card 2, check... until you hit it or run out. That "check every card" motion is the enemy this whole topic defeats.


1. The letter — "how many rows"

Why we need it: every cost we talk about ("how much work?") is measured as a function of . A shortcut that saves nothing when is small but saves everything when is huge is exactly what an index is — so we must be able to talk about growing.


2. Disk pages and I/O — the thing that is actually slow

This is why the parent keeps saying "the bottleneck is not CPU, it is disk I/O." Hold onto this: fewer page reads = faster query, full stop. See Disk vs Memory I/O.

Figure — Indexing — B-tree index, hash index, full-text

3. Big-O — a language for "how the cost grows"

Figure — Indexing — B-tree index, hash index, full-text

See Big-O Notation for the full treatment. For this topic you only need the ordering: , and to know that "full table scan" means = read every page.


4. The logarithm — the exact tool for "how many times can I split?"

The parent writes and . Let us earn every piece.


5. The letter — branching factor

You will meet again inside B+ Tree Data Structure; here just remember: large shrinks the height, and height = number of disk reads.


6. A "key" and a "pointer" — what actually sits in the index

Picture the book index at the back: the word is the key, the page number is the pointer. The index is small because it holds only these two things, not the whole rows.


7. "Sorted" and "order" — the property hash destroys

Keep this word close: a B-tree keeps keys sorted (so ranges and ORDER BY are cheap), while a hash deliberately throws order away (so it can't do ranges at all). Half the parent's comparison table is just this one distinction.


8. Hashing — turning a key into a bucket number

See Hashing for collisions and bucket details.


9. Words about words — tokenization, stemming, stop-words, postings

These four terms power the full-text section.


How it all feeds the topic

N rows in a table

cost = count of disk reads

disk page and I O

Big-O growth O(N) O(logN) O(1)

logarithm log_m N

branching factor m

key and row pointer

B-tree index

sorted order

hash index

hash function and bucket

tokenize stem stopwords

inverted index

full-text index

Indexing topic 4.4.13

This map is the parent note's skeleton: the left column (rows, pages, cost) is why indexes exist; and build the B-tree; the hash function builds the hash index; the word-tools build the full-text index. Master this page and the parent reads like a story you already know. Related deeper notes: Clustered vs Non-clustered Index, Query Optimization.


Equipment checklist

Cover the right side and answer each before opening the parent.

What does stand for in every cost formula?
The number of rows in the table.
Why do we count disk page reads instead of CPU comparisons?
Disk I/O is thousands of times slower than CPU; page reads are the real bottleneck.
Read out loud: , , .
Constant (flat), grows very slowly (gentle slope), grows in step with N (steep ramp).
What question does answer?
How many times must you multiply by itself to reach — i.e. how many tree levels.
Compute and explain the shortcut.
, using .
What does equal and why round up?
; a partly-needed level is still a whole extra level.
What is (branching factor) and why big is good?
Children per node; large makes the tree wide and shallow, so fewer disk reads.
What two things are in one index entry?
A key (the searched value) and a row-pointer (address of the full row).
Why can a hash index do equality but no ranges?
You compute the bucket directly (constant), but hashing scatters order so neighbouring values land far apart.
What does do in ?
Gives the remainder after dividing, wrapping the hash into a valid bucket number .
Define an inverted index and why it's "inverted".
Maps word → documents (flipping document → words), because a search gives a word and asks for documents.
Name the three text preprocessing steps.
Tokenization, stemming, stop-word removal.