Denormalization là trade-off có chủ đích: thêm redundant data để loại bỏ expensive JOINs — chỉ sau khi có profiling evidence; materialized view và cached columns là hai patterns phổ biến nhất.
Denormalization cố ý thêm redundant data để eliminate expensive JOINs — chỉ làm sau khi có profiling evidence, không phải premature optimization.
Patterns thực tế:
- Duplicate column — lưu
orders.user_emaildù đã cóusers.email→ report queries không cần JOIN users; - Computed/cached column —
users.post_countupdate khi insert/delete post thay vìCOUNT(*)mỗi lần; - Materialized view — pre-computed result set refresh định kỳ, PostgreSQL
CREATE MATERIALIZED VIEW+REFRESH MATERIALIZED VIEW CONCURRENTLY; - Flatten nested — embed thông tin address vào orders thay vì JOIN addresses (order history cần snapshot tại thời điểm mua, không phải địa chỉ hiện tại);
- Duplicate table cho different access patterns —
user_feedspre-computed thay vì JOIN users+posts+follows mỗi lần load feed
Trade-off: faster reads, slower writes, storage overhead, consistency risk — mỗi write phải update redundant data; dùng DB trigger hoặc application logic để maintain consistency.
Khi nào: analytics/reporting queries với phức tạp JOINs, read > 100:1 write ratio, response time requirement không đáp ứng được sau indexing.