Stop 10 · Schema Design & ConstraintsTopics · 10/11

Denormalization Trade-offs

Part of the SQL Roadmap

In short

Normalization keeps data clean, but sometimes a dashboard needs five joins to show one number. Denormalization copies some data on purpose to make reading fast, and you pay for it every time that data changes.

Denormalization deliberately repeats data to make reads faster or simpler, at the cost of extra storage and harder updates. It is common in reporting databases.

The jaadu sticks a handwritten paper list to a kitchen fridge door with a magnet, while holding a pencil to correct one line on it.

Think of it like this

The list on the fridge

Your family's phone numbers live in everyone's phones. But your mother also writes the important ones on a sheet stuck to the fridge in your Jaipur home, so she can find them in a second. It's faster to read. The catch: when your uncle changes his number, someone has to remember to fix the fridge sheet too.

Watch it happen

Normalized vs denormalized orders

Normalized

  • City stored once, on customers
  • Reports join orders to customers
  • One update fixes everything

Denormalized

  • City copied onto every order
  • Reports read one table, no join
  • Changes must update every copy

Design normalized first. Denormalize only for a measured read problem, and know how the copies will stay in sync.

The big ideas

What you need to know

Faster, simpler reads

Fewer joins means simpler and often faster queries, which matters for dashboards and reporting tables.

Harder writes

Every copy must be updated together. Miss one and two tables disagree about the same fact.

Sometimes the copy is correct

Storing the price on an order line is right: it records the price at purchase time, even if the product price changes later.

Let the database help

A materialized view stores a pre-joined result you refresh on a schedule, so you don't maintain copies by hand.

See it in code

Walk through the code

sql
1SELECT c.city, SUM(o.amount) AS revenue2FROM orders o3JOIN customers c ON c.customer_id = o.customer_id4GROUP BY c.city;5 6CREATE MATERIALIZED VIEW city_daily_sales AS7SELECT c.city,8       o.order_date,9       SUM(o.amount) AS revenue,10       COUNT(*)      AS orders11FROM orders o12JOIN customers c ON c.customer_id = o.customer_id13GROUP BY c.city, o.order_date;14 15REFRESH MATERIALIZED VIEW city_daily_sales;

1/3Normalized: city lives only on customers, so every report joins.

The normalized query, then a managed denormalized copy as a PostgreSQL materialized view. MySQL has no materialized views, so teams use a summary table filled by a scheduled job. SQL Server offers indexed views.

Save yourself the pain

Common mistakes

Oops

Denormalizing on day one 'for speed' before any query is actually slow.

Do this instead

Start normalized, add indexes, and measure. Denormalize only the part that's proven slow.

Oops

Copying customer city onto orders and never updating it.

Do this instead

Decide what the copy means. If it should track the latest city, update both. If it's 'city at order time', document that clearly.

Your mission

Weigh the trade-off

~20 min

Quick check

Why is storing unit_price on order_items usually correct, even though the product table has a price?

Go deeper

  1. 1List one case where storing a customer name on an orders table would help.
  2. 2List what could go wrong if that name changes.
  3. 3Notice that analytics tables are often denormalized on purpose.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge