← Back to SQL, page 11

sql-0202 · Medium mid

What is denormalization, and when is it a reasonable choice?

Denormalization stores redundant or precomputed data on purpose, such as an order_total column or a customer name copied onto invoices, to make reads faster or simpler. It is reasonable when reads greatly outnumber writes, when joins or aggregations are measurably slow, or when the copy is a historical snapshot. The cost is keeping copies consistent.

Full answer

Common, defensible forms:

  • Snapshot values: an invoice stores the customer's address and the price at the time of sale. This is not just a speed trick; the copy is the correct historical record.
  • Counters and totals: orders.total or posts.comment_count, maintained by the application, a trigger or a periodic job.
  • Reporting tables or materialized views that pre-join and pre-aggregate data for dashboards.

Before denormalizing, check that an index, a better query or a materialized view does not solve the problem. Then decide who keeps the copy correct and how drift is detected, for example a nightly check that recomputes totals and compares.

A useful interview framing: normalize the source of truth, then denormalize derived data where measurements justify it.

Key points

  • Denormalization adds deliberate redundancy
  • Historical snapshots are a legitimate copy
  • Counters and reporting tables speed reads
  • Someone must keep copies consistent

Common mistakes

  • Denormalizing before measuring
  • Copying live data without any way to detect drift

Follow-ups

  • How would you keep a comment_count accurate?
  • How does a materialized view compare with a denormalized column?

Tips

  • Separate snapshots from caches