4.4.22Databases

Denormalization — when and why

1,911 words9 min readdifficulty · medium

WHAT is denormalization?


WHY denormalize? (The trade-off, derived)

Let's reason from first principles instead of memorizing rules.

Suppose a query needs data spread across nn tables. To combine them the database performs joins. A nested-loop join of two tables of sizes RR and SS costs roughly O(RS)O(R \cdot S) rows examined without indexes, or O(RlogS)O(R \log S) with an index on SS. Chaining nn tables multiplies this work.

Figure — Denormalization — when and why

HOW: the common techniques


Worked examples


Common mistakes


Active recall

Recall Quick self-test (hide and answer)
  • What does denormalization trade, and in which direction?
  • State the inequality for when to denormalize.
  • Name three denormalization techniques.
  • Why must duplicated data be "guarded"?
  • Give one case where you should not denormalize.
Recall Feynman: explain to a 12-year-old

Imagine your toy box is super tidy: cars in one box, wheels in another, stickers in a third (that's normalized — nothing is repeated). But every time you want to play with a finished car, you must run to three boxes and assemble it — slow! So you keep a few fully-built cars ready on the shelf (that's denormalized — copies, fast to grab). The catch: if you repaint the real cars, you must remember to repaint the shelf copies too, or they'll look wrong. You only keep ready-made copies of the toys you play with all the time, not the ones you barely touch.


Connections

  • Normalization (1NF 2NF 3NF BCNF) — the thing we're partially undoing
  • Update Insert Delete Anomalies — the danger denormalization re-introduces
  • Indexing — alternative way to speed reads without duplicating
  • Materialized Views — cached-query form of denormalization
  • Database Triggers — common mechanism to keep duplicates in sync
  • OLTP vs OLAP — OLAP/warehouses (star schema) are heavily denormalized by design
  • CAP Theorem — distributed systems often denormalize to avoid cross-node joins
What is denormalization?
Deliberately adding controlled redundancy to a normalized schema to speed up reads, at the cost of write complexity and storage.
Which path gets cheaper and which gets costlier when you denormalize?
Reads get cheaper (fewer joins); writes get costlier (must keep duplicates in sync).
State the rule for when to denormalize.
When fr/fw>Csync/Cjoinf_r/f_w > C_{sync}/C_{join} — read/write ratio exceeds the sync-to-join cost ratio.
Name four denormalization techniques.
Pre-joined/merged tables, pre-computed derived columns, inline repeating/array columns, materialized views.
Why is a never-normalized schema NOT the same as denormalization?
Denormalization is a deliberate optimization of an already-normalized design; an un-normalized schema is just a missing design step.
What danger does denormalization re-introduce and how do you guard it?
Update anomalies / data drift; guard with triggers, transactions, app-layer invariants, or scheduled refresh.
Give a case where you should NOT denormalize.
Write-heavy, correctness-critical systems (e.g. a financial ledger) where fwf_w is high and sync must be exact.
Why does normalization make reads slow?
Data is split across many tables, so reads must JOIN them back together, and join cost grows with table sizes and number of tables.
Cheaper alternative to denormalization for speeding reads?
Proper indexing — it speeds reads without duplicating data or risking drift.

Concept Map

splits into

makes

forces

cost

adds controlled

risk

guarded by

shifts cost to

speeds up

worth it when

derived from

via techniques

Normalization

Small non-redundant tables

Writes safe

JOINs to re-assemble

Slow reads

Denormalization

Redundancy

Update anomaly

Triggers, app logic, refresh

Write path

fr/fw > Csync/Cjoin

Delta = fr*Cjoin - fw*Csync

Pre-joined tables, aggregates

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, normalization ka matlab hai data ko chhoti chhoti tables mein todna taaki koi cheez do jagah repeat na ho. Isse write safe rehta hai — ek fact sirf ek jagah, toh kabhi contradiction nahi hota. Lekin problem ye hai ki jab data padhna (read) ho, toh database ko sab tables ko JOIN karke wapas jodna padta hai, aur ye slow ho jaata hai, especially jab tables badi hon.

Denormalization isi ka ulta soch ke kiya jaata hai — hum jaan-boojh kar thoda redundancy (duplicate data) wapas daal dete hain taaki read fast ho jaaye. Jaise orders table mein total_amount directly store kar do, har baar order_items se SUM lagane ki zaroorat nahi. Cost kahin nahi jaati — bas read se hatke write par chali jaati hai, kyunki ab duplicate copy ko sync mein rakhna padta hai (trigger ya app logic se).

Decision ka simple rule: jab tum bahut zyada read karte ho aur kam write, tab denormalize karo. Formula yaad rakho: fr/fw>Csync/Cjoinf_r/f_w > C_{sync}/C_{join} — yaani read/write ratio cost ratio se bada hona chahiye. Banking ledger jaisi jagah jahan write frequent hai aur exactness critical hai, wahan denormalize mat karo.

Ek important baat — pehle hamesha normalize karo, fir evidence dekh ke denormalize karo. Pehle se hi duplicate banana (premature optimization) bugs laata hai. Aur jo bhi duplicate banao, usko trigger/transaction se guard zaroor karo, warna data "drift" ho jaayega aur galat dikhega. Mantra: "READ fast, WRITE last."

Go deeper — visual, from zero

Test yourself — Databases

Connections