SteelReed

Search

Search posts, questions…

SQL & Query TuningQuestion 20 / 37

Junior

What is an index, why does it speed up queries, and why do databases use B+trees?

Short answer

An index is an extra structure that keeps one or more columns' values pre-sorted, with pointers to the rows, so a lookup takes a few page reads instead of a full table scan. Databases use a B+tree because it is short and wide: a billion-row table needs only 3–4 levels.

linked to next leafpointerRoot pagehundreds of keysBranch pageLeaf: key + pointerNext leafRow in the table

3 points to say:

  1. Index = pre-sorted data + row pointers: a few page reads instead of a table scan.
  2. B+tree fan-out ~500, only 3–4 levels for a billion rows; linked leaves make range scans and sorting cheap.
  3. Indexes slow down writes; skip them for low-selectivity columns or small tables.

Keep reading the deep dive

Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.