Databaseschapter 2 of 5 · 4 lessons
Indexing
Pre-sorted data structures that trade write overhead for dramatic read speedup.
Your users table has 50 million rows. Somebody logs in, and your database looks up their email address by reading every single row until it finds the match.
That takes about 3 seconds, and it happens on every login. Add one line creating an index on that column and the same lookup takes 3 milliseconds, a thousand times faster, for four minutes of work.
It is the best return on effort anywhere in database performance. It is also not free, and the bill arrives on every write you ever make to that table, which is why index everything is exactly as wrong as index nothing.
Lessons
4 in this chapter- What an Index Actually IsA second, sorted copy of some columns, with pointers back to the real rows.3 min
- B-Trees, the DefaultHigh fan-out keeps any lookup within three or four page reads, even at billions of rows.3 min
- Composite and Covering IndexesColumn order inside an index is a design decision, not a detail.3 min
- What Writes Pay for Your ReadsEvery index is one more structure your INSERT has to update, forever.3 min