Course map

Topic 063 / Core

Normalization and denormalization

Chapter 5.1 · Data Modeling and Relational Databases

What and why

Normalization organizes tables so each fact is stored exactly once, eliminating update anomalies (the same fact drifting out of sync in two rows). Denormalization deliberately reintroduces duplication to make a specific read cheap, accepting the cost of keeping the copies consistent on write. The two are opposite defaults for opposite goals, not a hierarchy where one is "more correct." Builds on: → 59 (data modeling and entity relationships).

Diagram

Form Rule Fixes
1NF atomic columns, no repeating groups multi-valued fields
2NF no partial dependency on part of a composite key duplicate facts per key-part
3NF no transitive dependency (non-key depending on non-key) facts that should live on one table

How it works

  1. Start normalized (3NF is the usual target): one fact, one place.
  2. Identify the one query pattern that is actually slow because of a join that normalization requires.
  3. Denormalize only that path — duplicate the needed column(s) where the read happens.
  4. Name, in writing, what now must be kept in sync and how (application code, a trigger, or a materialized view).
  5. Prefer a materialized view over hand-maintained duplication when available — the engine keeps it consistent instead of your application code.

Practical example

From the catalog-plus-orders schema (topic 59), rendering an order history list needs the product name for every line item. Normalized, that's a join through order_item to product on every page view. Denormalizing by storing product_name and unit_price_cents directly on order_item at the time of purchase removes the join for that read and is correct for a different reason: an order's historical total must not change if the product is later renamed or repriced.

ALTER TABLE order_item ADD COLUMN product_name TEXT, ADD COLUMN unit_price_cents INT;

When to use it

  • Use when: a specific, measured query is slow because of a join, and index tuning (→ 64) alone doesn't fix it — or, as above, when the duplicated value must be historically frozen.
  • Avoid when: no query has actually been measured as slow — denormalizing speculatively just adds sync burden for no proven benefit.
  • Failure scenarios: a denormalized copy (product name, a cached count) drifts from its source with no process keeping it in sync (→ 81 names this explicitly).
  • Simpler alternative: a materialized view, refreshed on a schedule or on write — denormalized shape, engine-maintained consistency.

Practice

(20–30 min, lab Postgres — merges this chapter's catalog exercise). Build the catalog-plus-orders schema from topic 59 fully normalized (3NF) in the lab, seed it, and time a "customer order history" query. Then apply the product_name / unit_price_cents denormalization above, re-run the same query, and compare. Done when you can name, in one sentence, which query justified the denormalization and what must now be kept in sync.

Shared lab setup

Key takeaway

Normalize by default; denormalize one measured, named query at a time, and always say what now needs to stay in sync. Done when you can: justify a denormalization with a specific slow query, not a hunch. Used next in → 64 (indexing); data lifecycle for derived copies → 81; Projects 5, 6.

References

None — this topic makes no external claims.

Source directory

Search the guide