Denormalization
Denormalization in SQL is the deliberate repetition of data, such as storing a computed total or a copied column, to make reading faster at the cost of extra work when writing. It is used when a normalized query is too slow for how often it runs, for example a dashboard that shows every student's total on every page view. It isn't the same as a badly designed table: it's a normalized design with chosen, controlled copies. In this lesson, we will see what denormalization trades away, add a stored total and watch it go stale, and learn the tools that keep copies correct.
In One Minute
- Normalization stores each fact once; denormalization copies some facts on purpose.
- It makes reads cheaper and writes more expensive and riskier.
- Typical forms: a stored total column, a summary table, or a copied column that avoids a join.
- Every copy must be kept in sync: by triggers, by scheduled refreshes, or by recomputing.
- Normalize first; denormalize only a measured slow query.
Normalized vs Denormalized
| Normalized | Denormalized | |
|---|---|---|
| Each fact stored | Once | Sometimes more than once |
| Reads | May need joins and aggregates | Fewer joins, precomputed values |
| Writes | Change one place | Change every copy |
| Risk | Low | Copies can disagree |
A Stored Total
Instead of summing payments every time, store each student's total in students:
ALTER TABLE students ADD COLUMN total_paid NUMERIC(10,2) NOT NULL DEFAULT 0;
UPDATE students AS s
SET total_paid = coalesce((SELECT sum(amount_usd) FROM payments AS p WHERE p.student_id = s.student_id), 0);
SELECT student_id, first_name, total_paid FROM students ORDER BY student_id;ALTER TABLE
UPDATE 3
student_id | first_name | total_paid
------------+------------+------------
101 | Srihitha | 1700.00
102 | Advitha | 0.00
103 | Navika | 450.00
(3 rows)Explanation: Now SELECT total_paid FROM students is a simple read, with no join or sum. That's the benefit. The cost appears on the next payment.
The Copy Goes Stale
Each copy must be updated whenever its source changes. Record a new payment for Navika without touching the total:
ALTER TABLE students ADD COLUMN total_paid NUMERIC(10,2) NOT NULL DEFAULT 0;
UPDATE students AS s
SET total_paid = coalesce((SELECT sum(amount_usd) FROM payments AS p WHERE p.student_id = s.student_id), 0);
INSERT INTO payments (student_id, course_id, amount_usd, due_on, paid_on, method)
VALUES (103, 5, 25.00, '2026-01-05', '2026-01-05', 'card');
SELECT s.student_id, s.total_paid AS stored_total,
(SELECT sum(amount_usd) FROM payments AS p WHERE p.student_id = s.student_id) AS actual_total
FROM students AS s
WHERE s.student_id = 103;ALTER TABLE
UPDATE 3
INSERT 0 1
student_id | stored_total | actual_total
------------+--------------+--------------
103 | 450.00 | 475.00
(1 row)Explanation: The stored total still says $450.00; the payments add up to $475.00. This is exactly the update anomaly that Normalization removes, brought back on purpose, so it needs a plan to keep the copy correct.
Keeping Copies in Sync
| Method | How | Trade-off |
|---|---|---|
| Trigger | Update the copy inside the same transaction as the change | Always correct; slows writes; logic is hidden in the database |
| Materialized view | REFRESH MATERIALIZED VIEW on a schedule | Simple; data is stale between refreshes |
| Summary table rebuilt by a job | TRUNCATE and INSERT ... SELECT nightly | Easy to reason about; stale during the day |
| Recompute on read | Don't store it at all | Always correct; costs a query each time |
Triggers shows the first method on this exact total; Views covered materialized views. A good test: you should be able to drop every copy and rebuild it from the normalized tables at any time.
When Denormalization Pays
Denormalize when a read is frequent, slow and measured, and the data changes rarely by comparison. Good candidates:
- Dashboard totals read thousands of times a day, changed a few times.
- Reporting tables in a data warehouse, rebuilt nightly, where nobody writes during the day.
- A frequently shown name copied next to a foreign key to avoid a join on a very hot path.
Poor candidates are anything changed as often as it's read, or anything where a wrong value is costly, such as account balances that aren't kept by a trigger in the same transaction.
Common Mistakes
Denormalizing before measuring. Most joins on indexed keys are fast. Prove the query is slow first.
No sync plan. Every copy needs a trigger, a refresh schedule, or a rebuild job.
Confusing a messy design with denormalization. Repetition by accident isn't a performance choice.
Losing the source of truth. Keep the normalized tables; the copy should always be rebuildable from them.
Interview Corner
What is denormalization?
Intentionally storing redundant data, such as precomputed totals or copied columns, to speed up reads, accepting slower writes and the work of keeping copies consistent.
When would you denormalize?
When a frequent, measured-slow read can't be fixed with indexes or query changes, and the data changes rarely compared with how often it's read, such as reporting and dashboards.
How do you keep denormalized data consistent?
With triggers that update copies in the same transaction, scheduled refreshes of materialized views or summary tables, or by recomputing instead of storing.
Practice
Challenge: Build a summary table course_income with each course's title and total paid (coalesce(sum, 0)), using CREATE TABLE AS. Show it sorted by title. This is a snapshot: how would it get out of date?
Show solution
CREATE TABLE course_income AS
SELECT c.title, coalesce(sum(p.amount_usd), 0) AS total_paid
FROM courses AS c
LEFT JOIN payments AS p USING (course_id)
GROUP BY c.course_id, c.title;
SELECT * FROM course_income ORDER BY title;SELECT 6
title | total_paid
-------------------+------------
Git Basics | 0
Java Basics | 450.00
Java Collections | 550.00
Java OOP | 500.00
JavaScript Basics | 450.00
SQL Basics | 200.00
(6 rows)Every new payment changes the real total but not this table. It needs rebuilding, by a nightly job, a materialized view refresh, or triggers on payments. Until then, it reports yesterday's numbers.
Key Takeaways
| You want to | Use |
|---|---|
| A total that's always right | A trigger in the same transaction, or compute on read |
| A fast report, a bit stale | A materialized view or summary table on a schedule |
| To avoid a hot join | A copied column, kept in sync by a trigger |
Was this tutorial helpful?