← Back to SQL, page 24

sql-0475 · Medium mid

In an interview you are asked to estimate the storage for a table of one billion order rows. How do you approach it?

Estimate the average row size from column types (for example bigint 8 bytes, timestamp 8, a few integers, short text), add per-row overhead (about 24 bytes of header plus alignment in PostgreSQL), multiply by the row count, then add indexes, which often total 50 to 100 percent of the table, plus free space and growth. State assumptions and round sensibly.

Full answer

Worked estimate:

  • Columns: id bigint (8), customer_id bigint (8), placed_at timestamptz (8), status smallint (2), total_minor bigint (8), currency char(3) (about 4), channel short text (about 8). Roughly 46 bytes, say 48 with alignment.
  • Row overhead in PostgreSQL: tuple header about 24 bytes plus a 4-byte line pointer, so about 76 bytes per row.
  • Page fill: pages are 8 KB with some free space; assume about 100 rows per page, close to 80 bytes per row.
  • Table: 1,000,000,000 x 80 bytes is about 80 GB.
  • Indexes: primary key on id (about 20 to 25 GB), (customer_id, placed_at) (about 30 GB). Total about 55 GB.
  • Total: about 135 GB, plus bloat headroom (20 percent) and a year of growth.

Then discuss consequences: whether the working set fits in memory, backup and restore times, whether partitioning by time helps, and whether old data could move to cheaper storage. Interviewers value the method and stated assumptions over exact numbers. These figures are estimates, not measured here.

Key points

  • Estimate row size from types
  • Add per-row and page overhead
  • Indexes add a large share
  • Add headroom for bloat and growth
  • Discuss consequences, not just the number

Common mistakes

  • Forgetting indexes
  • Ignoring per-row overhead

Follow-ups

  • How would compression change the estimate?
  • How much memory would the working set need?

Tips

  • State each assumption