← Back to SQL, page 18

sql-0354 · Medium mid

Explain how a star schema for reporting differs from a normalised transactional schema.

A transactional (OLTP) schema is normalised to make small writes safe and consistent. A star schema for analytics has a central fact table of events with numeric measures, surrounded by denormalised dimension tables such as date, product and customer. It favours simple, fast aggregate queries over write efficiency.

Full answer

Example star schema for sales:

text
fact_sales(date_key, product_key, customer_key, store_key, qty, revenue)
dim_date(date_key, date, month, quarter, year, is_holiday)
dim_product(product_key, name, category, brand)
dim_customer(customer_key, name, city, segment)

Differences from OLTP:

  • Dimensions are denormalised: category and brand live on the product dimension instead of separate tables, so queries need one join per dimension.
  • The grain of the fact table is explicit, for example one row per order line.
  • Surrogate keys in dimensions allow history tracking, such as a customer moving city.
  • Loads happen in batches; updates to facts are rare.

Queries look like SUM(revenue) GROUP BY dim_date.month, dim_product.category, which columnar warehouses execute very quickly. A snowflake schema normalises dimensions further, trading simpler storage for more joins.

Key points

  • Fact tables hold measures at a stated grain
  • Dimensions are denormalised descriptive tables
  • OLTP schemas optimise writes and consistency
  • Star schemas optimise aggregate reads

Common mistakes

  • Running heavy analytics on the OLTP database
  • Leaving the fact table's grain undefined

Follow-ups

  • What is a slowly changing dimension?
  • How does a snowflake schema differ?

Tips

  • Define the grain