A write-up of Hussein Nasser’s video Best Practices Working with Billion-row Tables in Databases lists five strategies for huge tables: change the data model, index, partition, shard, and spread a brute-force scan over many workers. I agree that sharding comes late. I wanted numbers for four of its claims, so I ran them.
Short version: the index did almost all of the work, and partitioning paid off mainly when deleting old data. A range shard key on a growing id sent every new row to one shard, and appending to a million-entry follower array wrote 20,000 times more data than inserting a row.
I used PostgreSQL 17.12 from the postgres:17-alpine image with default settings, in Docker Desktop with 8 CPUs and 16 GB, on 22 November 2026. Times are the median of five warm runs. Pages are the 8 kB buffers reported by EXPLAIN (ANALYZE, BUFFERS). My tables hold 4.8 to 10 million rows, so every statement about a billion rows below is an extrapolation and says so.
1. One lookup in 5 million rows
A users table with 5 million rows (403 MB of heap) and one query:
SELECT * FROM users WHERE email = 'user4242424@example.com';
| Plan | pages | time |
|---|---|---|
| Parallel Seq Scan, no index | 51,546 | 89.5 ms |
| Index Scan, btree on email | 4 | 0.066 ms |
The index lookup read 4 pages: three index levels and one heap page. The index itself takes 24,789 pages (194 MB) with about 203 entries per leaf. With the same fan-out, 1 billion keys need 4 levels, so a lookup would read about 5 pages. A scan of 1 billion rows of the same width would read about 80 GB of heap (403 MB times 200). Both figures are extrapolated.
The index is about half the size of the table, and every insert has to update it. I add an index only for a query pattern I can see in the plans.
2. Partitioning: pruning worked, and a plain index was just as fast
I loaded 4.8 million orders into a table partitioned by month (12 partitions of roughly 370,000 to 410,000 rows) and into an ordinary table with the same data. Both have indexes on user_id and created_at.
| Query | partitioned | one table |
|---|---|---|
| count of March orders | 1 partition, 2,998 pages, 29.1 ms | 1,121 pages, 21.7 ms |
| orders of one user, no date in WHERE | 12 index scans, 44 pages, 0.27 ms | 1 index scan, 11 pages, 0.09 ms |
| remove January (about 400,000 rows) | DETACH 3.5 ms + DROP 8.2 ms | DELETE 422 ms |
Pruning worked: the March plan touches only orders_p_03. The unpartitioned table with an index still answered that query a little faster. A query without the partition key visits every partition, which cost four times the pages here. The PostgreSQL documentation warns that thousands of partitions slow down planning unless queries prune most of them (docs).
A primary key has to include the partition key, so the obvious definition fails:
ALTER TABLE orders_p ADD PRIMARY KEY (id); -- ERROR: unique constraint on partitioned table must include all partitioning columns -- DETAIL: PRIMARY KEY constraint on table "orders_p" lacks column "created_at" -- which is part of the partition key.
PRIMARY KEY (id, created_at) is accepted, and then the database no longer guarantees that id alone is unique.
The last row of the table is the reason I would partition. Dropping a month took about 12 ms in total. Deleting it row by row took 422 ms and left dead rows for vacuum to clean up. Each of those two was a single run. All twelve partitions sit on the same server, so partitioning adds no CPU, memory or disk bandwidth.
3. Sharding: the key decides the load
This one is a counting script, not a database. Four range shards hold 250 million user ids each, and 1 million new users arrive with ids starting at 760,000,001:
| Rule | shard 1 | shard 2 | shard 3 | shard 4 |
|---|---|---|---|---|
| range by user_id | 0 | 0 | 0 | 1,000,000 |
| md5(user_id) mod 4 | 250,649 | 249,302 | 249,934 | 250,115 |
With ranges over a growing id, every new user lands on the last shard. Hashing spreads them evenly. The price is that a range query such as users 760,000,001 to 760,000,100 now asks all four shards.
A lookup by email, when the shard key is user_id, also asks all four. The usual fix is a small directory table that maps email to user_id and is itself sharded by email. That directory can also enforce a unique email, which no single shard can. I did not build one here.
4. Following: a table or an array in the profile
The write-up compares a follows table with a list stored in the user’s profile. I built both: 10 million edges in follows(follower_id, following_id, created_at) with primary key (follower_id, following_id), and a profiles table with a bigint[] column.
| Query | plan | pages | time |
|---|---|---|---|
| who does user 42 follow (100 rows) | Index Only Scan on the primary key | 9 | 0.102 ms |
| who follows user 4242, no extra index | Parallel Seq Scan | 63,695 | 142.8 ms |
| same query, index on following_id | Index Scan | 7 | 0.061 ms |
The primary key already serves the first query, because it starts with follower_id. The expensive question is the reverse one, and it needs a second index. That index costs 22 bytes per edge: 93 bytes per edge with the primary key alone, 115 with both (0.93 GB and 1.15 GB for 10 million edges). If 100 million users each follow 300 accounts, that is 30 billion edges and about 3.5 TB at 115 bytes, extrapolated.
Storing the list in the profile makes the first query a single row read. Each new follow then rewrites the whole array:
| One new follow | stored size | WAL written | time |
|---|---|---|---|
| row in follows | 93 to 115 B | 215 B | 0.27 ms |
| append to array of 100 | 824 B | about 1 KB | 0.07 to 0.12 ms |
| append to array of 10,000 | 44 KB | 48 KB | 4.1 to 5.3 ms |
| append to array of 1,000,000 | 4.4 MB | 4.8 MB | 373 to 410 ms |
One follow costs 215 bytes of WAL as a row and 4.8 MB inside a million-element array, more than 20,000 times as much. (The first insert after a checkpoint wrote 7,624 bytes because of a full-page image; the other eleven wrote 215.)
Two other differences showed up. Appending the same id twice leaves it in the array twice, while the table rejects the second insert:
INSERT INTO follows(follower_id, following_id) VALUES (900001, 55); INSERT INTO follows(follower_id, following_id) VALUES (900001, 55); -- ERROR: duplicate key value violates unique constraint "follows_pkey"
The array also answers only one direction. Asking who follows X needs a second list for every user. For an account with 10 million followers at 8 bytes per id, that list is 80 MB, about five times MongoDB’s 16 MiB document limit. An array fits a short, capped list, such as a user’s pinned items. For followers I would keep the table.
5. The order I would try
- Read the plan of the slowest real queries with
EXPLAIN (ANALYZE, BUFFERS). - Add the index that plan asks for.
- Move old rows out of the hot table: partition by time, or archive them.
- Take read traffic off the primary with replicas or a cache, and check whether a bigger machine costs less than the next step. I did not measure this step.
- Shard when one primary cannot take the write load. Choose a hash key, and plan the lookups that do not use it.
The write-up puts a change of data model first. For a table that does not exist yet I agree. On a live system it means migrating every row, so I try it last. Brute-force scans over many workers belong to analytics, on a replica or a warehouse, and not in the request path.
Everything here ran on one machine with tables of 4.8 to 10 million rows. I did not measure cross-shard transactions, rebalancing, replication lag, or cold-cache behavior at a billion rows. Those are where sharding costs the most, and I would not trust a number from this setup for any of them.