Lesson 10 of 15
9. Indexes

Memory card and where the analogy breaks

3 min read+10 XP

The memory card (tear this page out)

#QuestionMemory code
9.1What is an index?A sorted copy that points to the page
9.2Scan vs lookup?Read every page, or jump to the page
9.3Index structures?Tree for ranges, hash for exact, LSM for floods of writes
9.4Composite indexes?Phone books find surnames, not first names
9.5Covering index?If the index has the answer, don't open the book
9.6Is my index used?Don't guess, EXPLAIN
9.7The write cost?Every index is a tax on every write
9.8When indexes don't help?Indexes help when you look up a few, by how the column starts

The whole chapter in one line: Index the few columns your hot queries search, in the right order, check with EXPLAIN, and pay the write tax knowingly.

Where the analogy breaks

  • A book's index is written once. A database index is rewritten on every change, thousands of times a second, which is why its write cost matters so much more.
  • A reader decides whether to use the index. In a database, the planner decides for you, based on statistics that can be wrong.
  • A book's index lists words. A database index can list combinations (user and date together), computed values (lower(email)), and even carry extra columns so the table is never touched.

Quick check

Five questions on this chapter. Pick an answer, then check it.

quiz