3 Hours

7. Denormalization: When and Why

Denormalization

Denormalization: When Normalization Goes Too Far

Normalization reduces redundancy and improves data integrity. But sometimes it goes too far. Denormalization is the process of intentionally introducing redundancy to improve performance. It's a trade-off: data integrity vs. performance.

Denormalization is appropriate when: you have read-heavy workloads, you need to reduce joins, or you need to optimize specific queries. It's also common in data warehouses and analytics databases. This module covers when and how to denormalize, and the risks involved.

When to Denormalize

Denormalize when performance is more important than strict normalization. Common scenarios: reporting and analytics, read-heavy applications, complex queries with many joins, and materialized views. Denormalization can dramatically improve read performance.

ScenarioNormalized (3NF)DenormalizedBenefit
Order ReportJOIN orders + usersuser_name stored in ordersNo JOIN needed
Product CatalogJOIN products + categoriescategory_name in productsSingle table query
SQL
-- Denormalized: Store user_name in orders table
CREATE TABLE orders_denormalized (
  id INT PRIMARY KEY,
  user_id INT,
  user_name TEXT, -- Denormalized field
  total DECIMAL(10,2),
  created_at DATE,
  FOREIGN KEY (user_id) REFERENCES users(id)
);

-- Query is simpler and faster
SELECT user_name, id, total
FROM orders_denormalized
WHERE created_at > '2024-01-01';

Managing Denormalized Data

Denormalized data must be kept in sync. This is the main challenge. Options include: triggers, application-level logic, or scheduled jobs. The choice depends on your requirements for consistency and performance.

  • Triggers: Automatic but can be complex and slow down writes.
  • Application logic: Manual but gives you control.
  • Scheduled jobs: Periodic sync, eventually consistent.
  • Materialized views: Database-managed denormalization.

The Risks of Denormalization

  • Data inconsistency: Redundant data can get out of sync.
  • Storage increase: Redundant data takes more space.
  • Complexity: The schema becomes harder to understand.
  • Write overhead: Updates become more complex.

Knowledge Check

Ready to test your understanding of 7. Denormalization: When and Why?

Back