Star Schema: Facts and Dimensions
One narrow table of things that happened, surrounded by wide tables describing them. The shape warehouses are built in.
Star Schema: Facts and Dimensions
Same shape, different slices
One narrow table of things that happened, surrounded by wide tables describing them. The shape warehouses are built in.
Same shape, different slices
A fact table records events or measurements, such as one order line, one payment, or one daily inventory snapshot. Its grain is the precise meaning of one row. A dimension table describes entities used to filter and group facts, such as customer, product, store, or date.
A fact table records events or measurements, such as one order line, one payment, or one daily inventory snapshot. Its grain is the precise meaning of one row. A dimension table describes entities used to filter and group facts, such as customer, product, store, or date.
A fact table can contain foreign keys to dimensions and numeric measures. Dimension tables are denormalized for convenient analysis. The layout resembles a star because dimensions connect to the central fact table.
Suppose one fact row represents an order line. If a query joins it to a dimension with multiple matching rows, the line can be duplicated and the sum inflated. Before aggregating, verify that each fact key matches at most one dimension row for the relevant point in time.
Choose measures that can be aggregated as intended. Revenue can often be summed; a precomputed average generally cannot be summed or averaged correctly without its count or denominator. Ratios should usually be recomputed from additive components.
A customer may move to a new region. A slowly changing dimension strategy determines whether historical facts use the old description, the new one, or both through versioned dimension rows. The choice must match the reporting question. A date dimension can also store calendar, fiscal, and holiday attributes that are awkward to derive repeatedly.
Star schemas are common in warehouses, but their details depend on the warehouse engine and modeling needs. See OLTP vs OLAP and normalization.
Suppose one fact row is one product line on one order. Joining a line to a product dimension should match one product version for the order's relevant date. If the dimension contains multiple historical versions and the join omits the effective date, the line may match multiple rows and revenue is doubled. Add a test that counts matched dimension rows per fact key. When totals change after adding a dimension, inspect join cardinality before assuming the aggregation is wrong.
A shared date or customer dimension can make reports across fact tables consistent, but only if its keys and definitions mean the same thing in each model. Joining two fact tables through dimensions can still multiply rows when both have many records per dimension key. Aggregate each fact to a compatible grain before combining measures. Document which dimensions are shared and which attributes are historical. Consistent naming does not guarantee compatible business definitions; the warehouse model should make those definitions explicit.
Answer without scrolling back up.
What does a fact table contain?
Long and narrow. The descriptive text lives in the dimensions, which are short and wide - putting it in the fact table widens the largest table for nothing.
Why is mixing grain in a fact table so dangerous?
It is the most common defect in real warehouses precisely because it is silent. State the grain in one sentence before creating the table.
Why do star schemas usually beat snowflake schemas in a warehouse?
The exception is a genuinely large dimension - tens of millions of customers - where duplication starts to matter more than the joins.
Build a star schema with one fact table and three dimensions, run an analytical query, and see why columnar engines prefer this layout.