ContentsThe library

Designing a Table to Live With

Copy the Value Only If You Are Also Building the Thing That Repairs It

Last timeThe Trouble With Missing

Deliberate duplication is a trade: reads get cheaper, writes get harder, and something has to keep the copy true. Here is the arithmetic and the two cases that are not really duplication.

Everything so far has pushed toward each fact living in one place. This lesson

argues the other side, which is a real argument with a real answer, and the

answer has a number in it.

Measure the read first

Start with the thing that is almost always skipped. Somebody says the orders

page is slow because it joins four tables, and proposes copying the customer

name onto the order. Before agreeing, find out what the join costs.

A join on an indexed key is among the cheapest operations a database performs.

It is a lookup in a tree, a few page reads, usually under a millisecond. Four

of them is still well under the time the page spends doing other things.

FIG 1Where the slow page was actually spending its time
stepwhat was measuredmillisecondsshare of the pagewhat happened
1the four joins everybody blamed32 per centThree milliseconds in total, all of it indexed lookups. Removing every one of them would make the page two per cent faster, which nobody would notice.
2one query run once per row of the list9463 per centFifty rows, fifty separate queries, each one fast. This is the real problem and it is a different problem: the fix is to fetch the rows together, which is a change to the code rather than to the schema.
3sorting a column with no index3825 per centAlso a real problem, also unrelated to duplication, and fixed by one index.
4everything else1510 per centRendering, serialisation, the network. The ordinary remainder.
4 steps
The joins were two per cent and were about to be designed away at the cost of permanent duplication in the schema. The two real causes were a query repeated per row and a missing index, both fixable in an afternoon without changing a single table. This trace is the most common shape of this conversation, which is why the measurement comes before the argument rather than after it.

If the measurement shows the join is genuinely the cost, the conversation is

worth having. Usually it shows something else, and the duplication would have

been permanent while the benefit was two per cent.

The lesson stops here

5 more paragraphs to go

You have read the opening. The rest of the argument, the problems that check whether it landed, and the lines worth keeping at the end all come with a plan.

The first lesson of every course in the library reads the whole way through, free, so you can see exactly what the rest of them are.

See the planThe contents

This is the reading half

Starting the course gives you your own copy of it. Every idea on every page has problems standing under it, marked with a reason rather than a tick, and any sentence you do not believe can be opened and argued with. None of that can happen on a page nobody owns.

The contents

The rest of this course

  1. 01Say Out Loud What One Row Is Claiming
  2. 02Seventy-Eight Copies and No Tiebreakeropening only
  3. 03The Email Address Was Reused by the New Employeeopening only
  4. 04There Is No Column That Can Hold a Listopening only
  5. 05The Query Returned Nothing and the Query Was Correctopening only
  6. 06Copy the Value Only If You Are Also Building the Thing That Repairs Ityou are here
  7. 07The Report Was Right in March and Is Wrong Nowopening only
  8. 08For Twenty Minutes, Both Versions Have to Be Rightopening only

Read alongside