BOOKS DEEP-READ · DATABASE INTERNALS · CH 1

Introduction and Overview: Taking the Database Apart

Database Internals · Ch 1 · Alex Petrov · 2019

中文 →

What is this chapter about?

You tap search in a shopping app and get results in a second; your colleague in finance runs a full-year report and waits half a minute. Both are "querying a database" — yet the machines behind them look nothing alike. Chapter 1 of Database Internals doesn't teach you to use a database. It takes the casing off so you can see, for the first time, which parts are inside and what each one does.

An analogy first

Think of a database as a large warehouse store. You hand in a shopping list: a greeter takes it; a clerk reads it and works out the least-walking route through the aisles; another clerk actually walks that route and picks the goods. And underneath all of that — the part that matters most — is how the shelves are arranged. That layer is called the "storage engine", and it is what the next several hundred pages of the book are about.

Why "databases" differ so wildly

Here's the odd part: MySQL, Redis and ClickHouse all call themselves databases, but hand the reporting job to one and the checkout job to another and you can be a hundred times slower. The difference isn't the brand — it's how the goods are shelved.

One arrangement is packing each customer's basket together: everything about one order sits side by side, so looking up a single order is one reach — great for checkout. The other is shelving by category: every order's "amount" in one pile, every "date" in another. To total the year's revenue you sweep the "amount" pile and never touch another shelf — great for reports, and because everything in a pile looks alike, it also compresses beautifully.

Then there's where you keep it: spread across the counter (memory), within arm's reach but expensive and gone the moment the power cuts; or stacked in the warehouse (disk), cheap and still there after a blackout, at the cost of a walk every time.

Three dials that produce nearly every database

The most valuable thing in this chapter is that the author hands you three "dials". Different settings give you essentially every storage design on the market.

One: batch it up, or not. Walk to the warehouse for every single item, or jot a few dozen on a sticky note and carry them over in one trip? Batching is far less work — but if that note goes missing you're in trouble, so you need a backstop.

Two: how you change things. Flip back to the original page and erase the old number, or write a new line at the end of the notebook and mark the old one void? The first saves space but you must find the page every time; the second is blazingly fast to write, at the cost of an ever-thicker notebook that needs periodic tidying.

Three: keep it sorted, or not. Alphabetised like a contact book, so everything starting with "S" is one continuous flip; or jotted down as it comes, found via an index. Sorted makes it easy to grab a whole stretch at once — but every new entry has to be slotted into the right place.

So how do you choose?

What this chapter really wants you to stop asking is "which database is best?" The question to ask instead is: does my job look at a handful of rows, or sweep a billion? Read-heavy or write-heavy? Is the hot data small enough to fit on the counter? The author also lands one uncomfortable warning: don't pick a database off someone else's benchmark — the workload they measured is almost certainly not yours.

The honest cost: none of the three dial settings is universally best — make writes faster and you generally pay it back in reads, in space, or in the tidying-up afterwards.

Remember this

A database isn't a black box; it's four layers: take the request → plan the route → go fetch → and how things are shelved. The bottom layer decides what it's good at. And shelving comes down to three dials: batch before writing or not, erase-and-rewrite or append a new line, keep it sorted or not. Choosing a database is really choosing a setting for those three.

Want the real anatomy, the magnitudes and the case studies? → switch to Deep