SQL · Queries that survive scale
When and how would you denormalize a schema?
Short answer
Denormalize when a read pattern is expensive and the duplicated data can be kept correct. Typical cases are reporting summaries, feed counters, and read models that avoid repeated joins.
Denormalization stores the same fact in more than one place, or stores a precomputed result. It trades write complexity and consistency risk for fewer joins and faster reads. It should be driven by a measured access pattern, not by a preference for wide tables.
ALTER TABLE orders
ADD COLUMN item_count INTEGER NOT NULL DEFAULT 0;
UPDATE orders o
SET item_count = (
SELECT COUNT(*)
FROM order_items i
WHERE i.order_id = o.id
);Maintenance strategies
- Update the copy in the same transaction as the source change
- Use a trigger for database-local invariants
- Use an outbox or event stream for cross-service read models
- Rebuild periodically with a reconciliation job and compare counts
- Store a source timestamp or version so stale data is detectable
The hard part is not writing the duplicated field, it is every path that changes the source. Enumerate all writers, make the update atomic where possible, and add monitoring for drift. For analytics, a materialized view is often a cleaner form of denormalization because it can be refreshed as a unit.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1How do you keep a denormalized count accurate?
- 2What consistency problems can denormalization introduce?
- 3When is a materialized view better than a duplicated column?
- 4How would you detect drift in a read model?
Say it in the interview
I denormalize when a read pattern is measurably expensive and I can maintain the duplicate correctly. Examples are counters, summary tables, and read models that avoid joins. The cost is that every write path must update the copy, so I define ownership and reconciliation. If the value is analytical, I prefer a materialized view that can be rebuilt.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live SQL mock interview and get scored feedback on depth, clarity and confidence.