Ankit KanojiyaIntermediate
Evergreen guide by Ankit Kanojiya · Intermediate · Databases
Data Modeling: Normalization vs Denormalization
How normal forms reduce redundancy, when denormalization pays off, and how to balance integrity against read performance.
- Data Modeling
- Normalization
- Schema Design
- Denormalization
- SQL
Data modeling decides how information is split across tables. Normalization removes redundancy to protect integrity; denormalization reintroduces some redundancy to speed up reads. Good schemas choose deliberately between the two.
What normalization solves
Normalization organizes columns so that each fact is stored exactly once. The goal is to eliminate update, insert, and delete anomalies, situations where redundant data drifts out of sync.
Consider a flat orders table that repeats the customer's address on every row. If the customer moves, every row must change, and missing one leaves conflicting addresses. Splitting customers into their own table fixes this.
The normal forms
The normal forms build on each other:
- First normal form (1NF): columns hold atomic values, with no repeating groups or arrays standing in for separate rows.
- Second normal form (2NF): every non-key column depends on the whole primary key, not just part of a composite key.
- Third normal form (3NF): non-key columns depend only on the key, not on other non-key columns.
Most operational schemas aim for 3NF. A normalized design for orders might look like this:
create table customers (
id bigint primary key,
name text not null,
city text not null
);
create table orders (
id bigint primary key,
customer_id bigint references customers(id),
total numeric(10,2) not null,
created_at timestamptz not null default now()
);
Here the customer's city lives in one place. Updating it touches a single row, and orders join to it when needed.
The cost of normalization
Normalized data must be recombined at read time with joins. For deeply related data, a single logical record can span many tables, and assembling it requires several joins that grow more expensive as data grows. Read-heavy systems can feel this cost on hot paths.
This is the core tension: normalization optimizes for correct, cheap writes and storage, while denormalization optimizes for cheap reads.
When denormalization helps
Denormalization stores redundant copies or precomputed values to avoid work at read time. Common techniques:
- Duplicating a frequently read attribute onto a child table to skip a join.
- Storing a computed aggregate, such as an order count, instead of recalculating it.
- Collapsing related entities into a single document or
jsonbcolumn when they are always read together.
alter table customers add column order_count integer not null default 0;
-- Maintained on each new order, often via a trigger or application logic.
update customers set order_count = order_count + 1 where id = 42;
The precomputed order_count turns an aggregate query into a single column read. The price is that this value can now disagree with reality if the maintenance logic has a bug.
Managing the trade-off
Denormalization trades write complexity and consistency risk for read speed. To keep it safe:
- Normalize first. Start from a clean 3NF model so the source of truth is unambiguous.
- Denormalize only where measurements show a real read bottleneck, not preemptively.
- Keep the derived data maintainable, ideally through triggers, materialized views, or a single well-tested code path.
- Document every redundant copy so future changes update all locations.
Materialized views are often the best compromise. They store a denormalized result that can be refreshed on a schedule, keeping the base tables normalized while serving fast reads.
create materialized view customer_order_stats as
select customer_id, count(*) as orders, sum(total) as revenue
from orders
group by customer_id;
refresh materialized view customer_order_stats;
Context changes the answer
Analytical and transactional systems pull in opposite directions. Transactional (OLTP) workloads favor normalization for integrity under frequent writes. Analytical (OLAP) warehouses often denormalize aggressively into star schemas because reads dominate and data is loaded in batches.
Summary
Normalization is the sound default: it keeps each fact in one place and makes writes correct. Denormalization is a targeted optimization applied after measurement, accepting controlled redundancy for faster reads. The skill is knowing which pressure a given workload faces and reaching for materialized views or derived columns only when the read cost is proven.