Books Deep-Read · DDIA · Chapter 2

Data Models and Query Languages

Designing Data-Intensive Applications · Ch 2 · Martin Kleppmann · 2017

中文 →

What's this chapter about?

A résumé you fill in, the whole web of friendships on your phone, the order you placed yesterday — when this data lands in a database, what shape does it actually take? DDIA (Designing Data-Intensive Applications) Chapter 2 argues this isn't a technical footnote — it's one of the most consequential choices in software design. The shape you imagine your data in decides how pleasant your code is to write and which questions are easy versus painful to ask.

A strange thing first

You'd assume "which database" is a chore you defer to the end. In fact it quietly decides, from day one, how smoothly your code flows. The same "user profile" can be stored like a stuffed folder, like a stack of tables cross-referenced by ID numbers, or like a web of who-knows-whom. Pick wrong, and you'll fight the database a little every day thereafter.

Three ways to store, three temperaments

The tables camp (relational): like a stack of spreadsheets. One row per record, cross-linked by ID — the way a library links "a book" to "who borrowed it" via a call number. Tidy, no duplication, but assembling the full picture means flipping between several tables.

The folder camp (document): everything about one user — name, every job, education, contacts — packed in one folder, pulled out in a single grab. Reading one profile takes one motion, fast; the downside is that linking one folder to another is clumsy.

The web camp (graph): the point isn't the dots at all — it's how the dots connect. Who follows whom, who's a colleague of whom, how to get from this stop to that one. Built for data where relationships are dense as a spiderweb — social networks are its home turf.

Why was this so awkward before?

Data in your code is "nested inside nested" (a person wrapping their several jobs), but old table databases are "flat, cell by cell." The two don't line up, so you hire a "translator" to shuttle back and forth, packing and unpacking — an awkwardness with a proper name: the "impedance mismatch." The folder camp exists largely to fire that translator: store the data in whatever shape it already has.

One more thing: how you ask

Having the data, you still have to query it — two styles. Imperative means stepping into the kitchen yourself and directing every move: grab this, flip that, loop and compare — you sweat the "how." Declarative (like SQL) means just naming your dish — "everyone surnamed Zhang living in Beijing" — and letting the kitchen (the database) figure out how to find them. The payoff is huge: the database can pick the fastest route on its own, and fire up several stoves at once (parallelism). That's why declarative has all but won over the decades.

So which should you pick?

Look at your data's shape and the questions you ask most: data like self-contained folders (rarely entangled) → the folder camp; data where anything can relate to anything (social, recommendations, road maps) → the web camp; even-handed and tidy relationships → the tables camp. There's no best, only best-fit. (One honest caveat: the folder saves you the pain of stitching tables — but the moment your data starts referencing itself heavily, it buckles, and you're back to stitching those joins by hand in your code.)

Remember this one line

The shape you store data in (tables / folders / web) is the deepest layer of software design — it decides how smooth your code is and how easy your questions are. And when you query, learn to "name the dish, don't step into the kitchen" (declarative) — leave the "how" for the database to optimize.

Want the actual models, query languages, and diagrams? → Switch to Deep mode