cloudpatterns.appClassroomKatasWhiteboardBlog
Z2 · Data & Storage

Relational databases & indexes

Relational databases & indexes

Bronze

A relational database keeps data in tables — rows with the same named columns — and links tables by keys, so one fact is stored once and SQL joins rows back together when you read. Without an index, finding a row means scanning the whole table; a B-tree index keeps the column sorted, so the database jumps to the row in a few page reads, even among millions. Indexes are not free — every insert, update and delete must also update each index, and each one takes disk space. A multi-column index helps only queries that use its leftmost columns, and EXPLAIN shows whether a query really uses the index or scans.

Use when
Data with clear relationships that many parts of the app read in different ways · Updates that must change several rows together, all or nothing — like moving money between balances · Queries you can't fully predict up front — SQL can ask new questions of the same tables
Avoid when
Indexing a column that is rarely searched but often written — the index costs more than it saves · Indexing a tiny table — scanning a page or two is already as fast as the index · Joining many large tables on every request at high traffic — measure it, or shape the data for that read
Trade-offs
Each index makes some reads much faster and every write a little slower · Normalized tables store each fact once, but reads pay for joining them back together · A wide, multi-column index serves more queries but takes more space and more write work
Requires
Databases: the system of record
3 objectives · 6 misconceptions
  • predict Predict how a query's latency changes when it scans a table instead of using an index, and as the table grows.
  • explain Explain why each extra index makes writes slower and the database bigger, and when an index is not worth keeping.
  • choose Choose which column, or which order of columns, to index for a given query — and read a query plan to check the index is used.
  • Not true: Adding an index to every column makes the database faster.
  • Not true: Indexes only speed things up — they cost nothing on writes.
  • Not true: An index on (A, B) helps a query that filters only on B just as much as one on A.
  • Not true: Once a column has an index, every query on that column uses it.
  • Not true: Joins are always slow, so a relational design should avoid them.
  • Not true: A query that is fast on today's table stays fast as the table grows.

Learn

Relational databases & indexes~13 min

Practice

No katas practice this yet.

Check

Station test-out80% to pass

Neighborhood

Relational databases & indexes
contrast withNoSQL families
On the pathChapter 11 · Truth at scale

Progress is saved on this device