Skip to content
Ameen Abozaid
MID-019

Data modeling at scale

  • 6 Lessons
  • 2.5 h practice (estimate)
On this page 8 topics

Overview

Topics

  • Normalization trade-offs
  • denormalization
  • partitioning

Learn first

2.5 h of practice (estimate)

Learning outcomes

  1. Decide when to denormalize for read performance and how to keep copies consistent.
  2. Partition a large table by range or list and explain partition pruning.
  3. Use jsonb columns appropriately and index them.
  4. Plan data retention and archiving.

Lessons

  1. 01Normalization trade-offs
  2. 02Denormalization patterns
  3. 03Table partitioning
  4. 04jsonb: when and how
  5. 05Retention and archiving
  6. 06Demo: partitioning the order history by month

Lab and exercises

Lab

MID-019-LAB1 · Partitioned orders

Outcomes: 1–4 · Time: ~2.5 h Partition the 10 M-row order table by month. Compare query time for a one-month report and for a customer lookup before and after, and compare the cost of removing one old month (detach versus DELETE). Add a materialized view for daily sales and refresh it concurrently. Acceptance: pruning visible in plans; old partitions detachable; comparison table of timings.

Exercises (assessment: modeling scenario)

IDOutcomeTask
MID-019-EX011For a slow report needing five joins, propose one denormalization and name how you keep it consistent.
MID-019-EX021Decide which of these are duplication and which are valid snapshots: product_name in order items, customer_email in orders, price_at_purchase.
MID-019-EX032Create a range-partitioned table with three monthly partitions and a default partition.
MID-019-EX042Write a query that prunes to one partition and one that cannot, and show both plans.
MID-019-EX053Store variable product attributes as jsonb, index them, and query by containment.
MID-019-EX063List three fields in a jsonb-heavy design that should become columns.
MID-019-EX074Write a retention plan for orders, audit log, and sessions with archive method for each.
Show solutions
  • EX01 For example a daily_sales materialized view refreshed nightly or an order_total column maintained in the same transaction; state the allowed staleness.
  • EX02 price_at_purchase and arguably product_name are snapshots; customer_email in orders is a copy that should be justified (snapshot of the address used) or removed.
  • EX03 PARTITION BY RANGE (created_at), three FOR VALUES FROM ... TO ... partitions, and CREATE TABLE ... PARTITION OF orders DEFAULT.
  • EX04 WHERE created_at >= '2026-02-01' AND created_at < '2026-03-01' scans one partition; WHERE customer_id = 7 scans all partitions (no key filter).
  • EX05 attrs jsonb, CREATE INDEX ... USING gin (attrs jsonb_path_ops), WHERE attrs @> '{"color":"red"}'.
  • EX06 Foreign keys, status fields used in filters and joins, amounts used in calculations.
  • EX07 For example orders: keep hot 2 years in the database, archive older partitions to files; audit log: monthly partitions kept 1 year; sessions: delete after 30 days. Values are examples; check reasoning.
  • The "partitioning small tables" and "jsonb as schema avoidance" mistakes are covered by EX06 and the quiz.

Quiz

Outcomes: O1 normalization and denormalization · O2 partitioning · O3 jsonb · O4 retention.

A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.

  1. 1Denormalization is justified by:O1
  2. 2REFRESH MATERIALIZED VIEW CONCURRENTLY requires:O1
  3. 3price_at_purchase stored in an order item is:O1
  4. 4Partition pruning happens when the query:O2
  5. 5In a range-partitioned table, the primary key must:O2
  6. 6A good reason to partition by month:O2
  7. 7The index type usually used for jsonb containment:O3
  8. 8jsonb is a poor choice for:O3
  9. 9Best way to remove a year of old orders from a partitioned table:O4
  10. 10Archived data should be:O4

Practical part

Modeling scenario: given a growing orders-and-events schema, decide what to denormalize, what to partition (and by what key), what goes in jsonb, and the retention plan. Pass: quiz 8/10, a scenario answer that justifies each decision, and the lab showing pruning.

Common mistakes

Partitioning small tables; jsonb as a schema-avoidance strategy.

Assessment

Modeling scenario.

Career relevance

Sharding and hot partitions (SRC-007).

Type to search levels, phases, modules and lessons.