Tinker Databases & indexes

by Kondasamy Jayaraman · drawn with Colorfigures by April Zhu

Costliest request: one order by number, 200 pages each, 2,000 a day.200 pages read · 10,000 rows read

Chapter 7 of 7

Plan Larkspur’s indexes

Everything is on the bench: a day of requests, the new orders, five possible indexes and a budget for writes.

This chapter: Which indexes make the day’s reads cheapest within the write budget?

About this chapter

You will learn

  • How to weigh reads against writes
  • Why one index can serve two requests
  • Why some indexes aren’t worth their writes

Words you’ll meet

Workload
The requests and writes a database serves in a day, with how often each comes.
Write budget
How many pages a day the team lets new orders cost, so checkout stays quick.

Step 1 of 2

The is the day’s requests and 300 new orders. The : 4,300 pages a day for writes.

Pick the best indexes.

528,800 pages read a day BEST IS 21,240

The day’s requests
RequestA dayWayPages a day
One order by number2,000Full scan400,000
A customer’s orders400Full scan80,000
A customer’s orders this month200Full scan40,000
Today’s orders40Full scan8,000
Returned orders4Full scan800
New orders300Writes300 of 4,300

The order system may spend at most this many page writes a day on new orders, so checkout stays quick in the rush.

The SQL
-- no indexes

A teaching database: Larkspur’s generated orders in a real B-tree, pages counted as it runs. Not any real database’s numbers.