← Back to SQL, page 11

sql-0208 · Medium mid

When would you use a star schema, and how does it differ from a normalized OLTP schema?

A star schema has a central fact table of measurable events (sales lines with quantity and amount) surrounded by denormalized dimension tables (date, product, customer, store). It suits analytical queries that filter and group by many attributes. OLTP schemas are normalized for consistent, concurrent writes; star schemas trade that for simple, fast reads.

Full answer

Fact table: one row per event at a chosen grain, such as "one row per order line". It holds foreign keys to dimensions and numeric measures.

Dimension tables: descriptive attributes, often flattened. A product dimension might carry category and brand directly instead of separate tables, so a report needs one join per dimension, not a chain.

A date dimension (one row per day with year, quarter, week, holiday flag) makes calendar grouping a simple join and keeps business definitions such as fiscal quarters in one place.

The design steps commonly taught:

  1. Choose the business process.
  2. Declare the grain.
  3. Pick dimensions.
  4. Pick facts (measures).

Getting the grain wrong is the classic mistake. Mixing order-level and line-level facts in one table double-counts order shipping fees.

Key points

  • Fact tables hold events and measures at a declared grain
  • Dimension tables hold descriptive, often denormalized attributes
  • A date dimension simplifies calendar logic
  • OLTP favours normalized writes; star schemas favour reads

Common mistakes

  • Mixing grains in one fact table
  • Snowflaking dimensions without need and adding joins

Follow-ups

  • What is a slowly changing dimension?
  • How would you model shipping fees charged per order?

Tips

  • Declare the grain first