Quick revision
The short answer
I would compare three options. A PostgreSQL hash index on url stores only 4-byte hash codes and row pointers, so it is far smaller and more likely to stay cached. A fixed-size digest column, such as a 16-byte md5, with a B-tree index works in both PostgreSQL and MySQL and can carry a unique constraint, as long as queries compare both the digest and the url. And normalising urls into a deduplicated lookup table with an integer key shrinks the big table too. I would choose by measuring index size, cache hit ratio and p99 latency on realistic data.
What to remember
In this answer, we saw that a 140 GB B-tree on long urls is large because it stores every key, while the workload only needs equality. A PostgreSQL hash index stores hash codes and pointers and is far smaller, a digest column with a B-tree is similar in size, works in both PostgreSQL and MySQL and can be unique as long as queries also compare the url, and a deduplicated lookup table with integer keys shrinks the large table too. We decide by measuring index size, cache hit ratio and p99 latency on realistic data.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
I would compare three options. A PostgreSQL hash index on url stores only 4-byte hash codes and row pointers, so it is far smaller and more likely to stay cached. A fixed-size digest column, such as a 16-byte md5, with a B-tree index works in both PostgreSQL and MySQL and can carry a unique constraint, as long as queries compare both the digest and the url. And normalising urls into a deduplicated lookup table with an integer key shrinks the big table too. I would choose by measuring index size, cache hit ratio and p99 latency on realistic data.
Why this question is asked
This is a classic case of an index that is correct but too big. A 140 GB B-tree that does not fit in memory turns every lookup into several random disk reads, and the table only ever needs exact matches, so most of what the B-tree offers, ordering and ranges, goes unused. Interviewers ask this scenario to see whether we can reason from the shape of the workload to the shape of the index. In this answer, we will see why the B-tree is so large, then compare the hash index, the digest column and the lookup table, handle collisions, and plan the measurements.
1Why the B-tree Is 140 GB
A B-tree stores the whole key in every leaf entry. With 300 million urls of about 400 bytes each, the leaf level alone needs roughly 300 million times 400 bytes, about 120 GB, plus entry headers, pointers and free space in pages, which brings it to 140 GB. Long values also mean fewer separators fit in each inner page, so the tree is deeper than it would be for short keys.
If the server has, say, 64 GB of memory, most of the index is on disk. A lookup needs one or two leaf and inner pages that are probably not cached, and at a few thousand lookups per second those random reads dominate latency. Since the query is always url = ?, we are paying for ordering that we never use.
2Option 1: A PostgreSQL Hash Index
A hash index stores a 4-byte hash code and a row pointer per entry, independent of the key length. For 300 million rows that is roughly 20 bytes per entry including overhead, so somewhere around 6 to 10 GB, small enough to stay in memory on our server. A lookup hashes the url, reads the bucket, then checks the url in the matching rows.
The limits matter here. A PostgreSQL hash index cannot enforce uniqueness, so if urls must be unique we still need something else for that. It is PostgreSQL-only, and it covers only one column. Since PostgreSQL 10 it is crash safe and replicated, so durability is no longer a concern.
3Option 2: A Digest Column With a B-tree
The second option builds the small key ourselves. We add a column holding a fixed-size digest of the url and index that with an ordinary B-tree. md5 produces 16 bytes, which PostgreSQL can store as a uuid, and MySQL as BINARY(16). The B-tree on 16-byte values is about the same size as the hash index, and because it is a B-tree, it can be unique.
- Compute md5 of the url16 bytes
- Descend the small B-treeabout 10 GB, mostly cached
- Any row with this digest?YesFetch the rowNoNot found
- Does the stored url equal the search url?YesReturn itNoTreat as not found: a collision
4 steps in order. Play them, or step through them one at a time.
Notice the second condition in the query. The index finds rows by digest, and the url comparison confirms that the row is really the one we asked for. Because the digest is a generated column, the application cannot forget to fill it. Adding a stored generated column rewrites the table, so on 300 million rows we would rather add a plain column, backfill it in batches, and add the index concurrently.
4Option 3: Normalise Into a Lookup Table
The third option asks whether 300 million rows really hold 300 million different urls. In many systems, such as click logs or crawl records, the same urls repeat many times. Then we can move the urls into a table of their own, store each distinct url once with a bigint id, and keep only that 8-byte id in the large table.
This shrinks the big table by about 400 bytes per row, which is around 100 GB, and its indexes too. The lookup table needs its own index on url, by hash or digest, but it is only as large as the number of distinct urls. The cost is an extra lookup or join and a little more work on insert, where we must find or create the url's id. We can also normalise the urls first, for example lowercasing the host and removing tracking parameters, if the business agrees that those variants are the same page.
5Measuring Before We Decide
Each option makes a different trade-off, so we test them on a copy of real data, not on a small sample, because cache behaviour only appears at full size. For each, we record the index size with pg_relation_size or the MySQL index statistics, the cache hit ratio during a replayed lookup workload from pg_stat_database or the buffer pool counters, and the p50 and p99 lookup latency. We also measure write cost, because each option adds work on insert.
| Option | Index size | Unique possible | Works in MySQL | Main cost |
|---|---|---|---|---|
| B-tree on url (today) | about 140 GB | yes | yes | does not fit in memory |
| Hash index on url | single-digit GB | no | no | PostgreSQL only |
| md5 digest + B-tree | about 10 GB | yes | yes | extra 16-byte column |
| Lookup table of urls | depends on distinct urls | yes | yes | join, insert logic |
6Follow-up questions
How do you handle hash collisions in the digest approach?
Always compare the full url as well as the digest in queries, so a collision returns no wrong row. With a 128-bit md5, an accidental collision among 300 million urls is so unlikely that a unique index on the digest is reasonable. MD5 is broken against deliberate attacks, though, so if users can submit urls and a forced collision could block a legitimate insert, use a truncated SHA-256 instead. With a 64-bit digest the picture changes: across 300 million values the chance of at least one collision is roughly a quarter of a percent, so the digest index must not be unique and the url check is essential.
Would PostgreSQL B-tree deduplication help?
Deduplication, added in PostgreSQL 13, stores repeated keys once per leaf page. It helps when many rows share the same url, but for mostly distinct urls it saves almost nothing. A lookup table helps the repeated case more.
Could we partition the table instead?
Partitioning by hash of url splits the index into smaller pieces, but the total size is the same and every lookup still needs its piece in memory. It helps maintenance, not the working set.
7Conclusion
In this answer, we saw that a 140 GB B-tree on long urls is large because it stores every key, while the workload only needs equality. A PostgreSQL hash index stores hash codes and pointers and is far smaller, a digest column with a B-tree is similar in size, works in both PostgreSQL and MySQL and can be unique as long as queries also compare the url, and a deduplicated lookup table with integer keys shrinks the large table too. We decide by measuring index size, cache hit ratio and p99 latency on realistic data.
Open with
The B-tree is huge because it stores the whole 400-byte url in every entry, and we only ever do exact matches, so we pay for ordering we never use.
Then, if they want more
I would compare three options. First, a PostgreSQL hash index on url: it stores a 4-byte hash code and a row pointer per entry, so it would be in the single-digit gigabytes and likely stay in memory. Since PostgreSQL 10 it is crash safe and replicated, but it cannot enforce uniqueness and it is PostgreSQL-only.
Second, a digest column: a 16-byte md5 of the url stored as uuid in PostgreSQL or BINARY(16) in MySQL, filled by a generated column or a backfill, with a B-tree index. It is about as small, works in both databases, and can be unique.
The query compares both the digest and the url, so a collision never returns a wrong row; with a 64-bit digest I would not make it unique, because collisions become plausible at 300 million rows. Third, if urls repeat, normalise them into a lookup table with a bigint id, which also shrinks the big table by roughly 100 GB.
I would test each on a full-size copy and measure index size with pg_relation_size, cache hit ratio and p99 lookup latency, plus insert cost, before deciding.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- MySQL 8.4, PostgreSQL 10, PostgreSQL 13
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, MySQL reference manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A table stores 300 million rows with a url column averaging 400 bytes, queried only by...": https://scrivio-interview-library.netlify.app/databases/indexing/ee1aa0b8a3/