Home›Learn›Start 4: napkin math
Course · FoundationsLesson 4 of 330 read

Database napkin math

How big will this table be, and what will it cost to run?

One worked example in eight steps, and the rules of thumb behind it.

Press Play, or step with the arrows.

Napkin math is how you find out, before writing code, whether a design fits on one server. For a database it comes down to a few quantities: bytes per row, rows per page, bytes of index per row, bytes of log per change, and how fast one disk flush is.

The numbers worth remembering

Go deeper
QuantityRule of thumbWhere it comes from
Page8 KB in PostgreSQL, 16 KB in InnoDBThe unit every engine reads and writes
Row overheadAbout 28 bytes in PostgreSQL, about 20 in InnoDBHeader plus pointer, per row
bigint primary key indexAbout 22 bytes a rowNotebook 10: 21.4 MB per million rows
UUIDv4 primary key indexAbout 40 bytes a rowNotebook 10: 38.4 MB per million, pages 70% full
WAL per insertOften more than the row itselfNotebook 10: 235 MB for 1,000,000 rows of 145 bytes
Durable commit, one connectionA few thousand a second, set by the disk flushNotebook 2: 0.236 ms per commit
Index lookupA handful of pages, whatever the table sizeNotebook 3: 4 pages in 1,000,000 rows
Full scanEvery pageNotebook 3: 19,231 pages, 158 MB

Notebook 3 measured a users table, a different row size from the orders row above, which is why its page count does not match the 56 rows a page worked out here. These are from one GitHub runner with stock settings. They are good enough to tell whether a design needs one server or ten, which is what napkin math is for. Before you buy hardware, run the same insert on your own disk.

Check the estimate on a real table

Go deeper
SELECT pg_size_pretty(pg_relation_size('orders'))       AS table_size,
       pg_size_pretty(pg_indexes_size('orders'))        AS indexes,
       (SELECT reltuples::bigint FROM pg_class WHERE relname = 'orders') AS rows_estimate;
-- WAL written in the last minute: run twice, 60 seconds apart
SELECT pg_current_wal_lsn();
SELECT pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), '<first value>'));
← Chapter 3: How a query travelsYou finished the Start trackBack to chapter 1Next, pick a database in the Learn menu and follow its chapters.
Connected

Where this shows up

The same mechanism, sharding and partitioning, keys and ids, elsewhere on the site: in real incidents, in measurements, and in designs.

Check yourself · 3 questions

Check yourself

Pick an answer. You will see whether it is right, and why.

1. A 145-byte row. About how many fit in one 8 KB PostgreSQL page?

2. Which is usually bigger per insert in PostgreSQL?

3. One connection, one insert per commit, 0.236 ms per log flush. Roughly what is the ceiling?

One database decision a weekA short email each week: one real decision, the trade-off, and the command to check it. Unsubscribe in one click.

Part 1 of 4 · Learn · 56 pagesAll pages

How do databases actually work? From one row on disk to transactions: the words you need, PostgreSQL, MySQL and MongoDB from the files up, then Redis, Cassandra and vector search, side by side, and the query and concurrency traps.

Start 1: the basicsStart 2: ten conceptsStart 3: how a query travelsStart 4: napkin mathThe courseShort videosPostgreSQL 1: why PostgreSQLPostgreSQL 2: how it stores your dataPostgreSQL 3: what happens on every writePostgreSQL 4: high availability with PatroniPostgreSQL 5: running it as a DBREPostgreSQL 6: when it breaksPostgreSQL on Kubernetes: CloudNativePGMySQL 1: why MySQLMySQL 2: how it stores your dataMySQL 3: what happens on every writeMySQL 4: high availability with InnoDB ClusterMySQL 5: running it as a DBREMySQL 6: when it breaksMySQL failover with OrchestratorMySQL on Kubernetes: the Percona operatorsMongoDB 1: why MongoDBMongoDB 2: how it stores your dataMongoDB 3: what happens on every writeMongoDB 4: high availability with replica setsMongoDB 5: running it as a DBREMongoDB 6: when it breaksMongoDB on Kubernetes: the Percona operatorB-tree and LSM treeRedis 1: why RedisRedis 2: how it stores your dataRedis 3: high availabilityRedis 4: running it as a DBRERedis 5: when it breaksCassandra 1: why CassandraCassandra 2: how it stores your dataCassandra 3: high availabilityCassandra 4: running it as a DBRECassandra 5: when it breaksVector databases 1: why vector searchVector databases 2: how it stores your dataVector databases 3: high availabilityVector databases 4: running it as a DBREVector databases 5: when it breaksDatabase familiesEngines side by sideQueries that look fine and are notLost updates and write skewMany servers 1: keep a copy runningMany servers 2: who is the leaderMany servers 3: CAP as settingsMany servers 4: unique IDsMany servers 5: split the dataMany servers 6: across servicesMany servers 7: a thousand databasesStaff path, zero to architect