Star Schemas
The Star Schema
Structure data for fast analytical queries
Facts in the Center, Dimensions Around Them
Star Schema vs Snowflake Schema
- Dimensions are fully denormalized (flat)
- Fewer JOINs per query
- Simpler for analysts to understand
- Standard in most data warehouses
- Dimensions are normalized (sub-tables)
- More JOINs per query
- Less storage duplication
- Useful when dimension hierarchies change independently
Why Star Schemas Are Fast
Types of Fact Tables
Choose transaction, snapshot, or accumulating
Transaction Facts
Periodic Snapshots
Accumulating Snapshots
Factless Facts
- One row per event
- Never updated
- Additive measures (quantity, amount)
- Cannot answer 'current state' queries
- One row per entity per period
- Never updated (new snapshot each period)
- Semi-additive measures (balance, level)
- Answers 'what was the state on date X?'
Types of Dimensions
Build descriptive lookup tables
Conformed Dimensions
Role-Playing Dimensions
Junk Dimensions
Degenerate Dimensions
The Date Dimension
Defining the Grain
Set the exact level of detail per row
How to State the Grain
| Precision | Grain Statement | Quality |
|---|---|---|
| Vague | "It's an orders table" | Unclear what 'order' means |
| Better | "One row per order" | What if an order has multiple items? |
| Good | "One row per order line item" | Clear unit of analysis |
| Best | "One row per order line item, unique on (order_id, line_seq)" | Enforceable with a constraint |
Why Grain Matters
- If the grain is order-line-item, product_sk makes sense. If the grain is order, product_sk does not (which product? there are multiple).
- quantity and line_amount are additive at the line-item grain. They must be pre-aggregated at the order grain.
- Joining a line-item grain fact to a customer dimension is safe (one customer per order). Joining to a product dimension may fan out if the grain is order, not line item.
Grain Validation
- Multi-item orders have one row
- product_sk is ambiguous (which product?)
- SUM(amount) correct per order but wrong per item
- Cannot do product-level analysis
- Each line item is its own row
- product_sk is unambiguous
- SUM(amount) correct at every level
- Supports order-level AND product-level analysis
Surrogate Keys
Generate stable IDs independent of source data
Why Not Just Use Natural Keys?
How Surrogate Keys Enable SCD Type 2
Surrogate Key Assignment
> You are building an analytical model for an e-commerce platform. The business needs daily revenue dashboards and lifecycle tracking for order fulfillment.
Stars, snowflakes, and facts between
- Category
- Data Modeling
- Duration
- 30 minutes
- Challenges
- 12 hands-on challenges
Topics covered: The Star Schema, Types of Fact Tables, Types of Dimensions, Defining the Grain, Surrogate Keys
Lesson Sections
- The Star Schema (concepts: dmStarSchema)
Facts in the Center, Dimensions Around Them A star schema has one table in the middle (the fact table) surrounded by several tables around it (dimension tables). The fact table stores measurable events: a sale happened, a click occurred, a payment was processed. Dimension tables store the descriptive context: who (customer), what (product), when (date), where (store). The fact table is tall and narrow: billions of rows, each with a few FK columns pointing to dimensions plus a few numeric measure
- Types of Fact Tables (concepts: dmFactTables)
There are three fundamental types of fact tables. Each models a different kind of business process. Choosing the wrong type means your table cannot answer the questions it was built for. Transaction Facts One row per discrete business event: a sale, a click, a payment. Transaction facts are insert-only. Rows are never updated. They are the most common fact type and the right choice for any event-level data. Transaction facts cannot answer 'what is the current state?' questions. They record event
- Types of Dimensions (concepts: dmDimensionTables)
Dimensions are the descriptive lookup tables that give meaning to fact rows. A fact row says 'customer_sk = 42, amount = $100.' The dimension tells you customer 42 is 'Alice Zhang from Seattle in the Enterprise segment.' Without dimensions, facts are just numbers. Conformed Dimensions A conformed dimension is shared across multiple fact tables. dim_customer is referenced by fact_sales, fact_returns, fact_support_tickets. The same customer_sk and the same attribute definitions. This is what makes
- Defining the Grain (concepts: dmGrainDefinition)
The grain is the most important decision in dimensional modeling. It defines what one row in the fact table represents. 'One row per order line item.' 'One row per click event.' 'One row per daily account balance.' Everything else follows from this: which dimensions make sense, which measures are valid, which queries produce correct results. How to State the Grain A grain statement is precise enough that another engineer can write a uniqueness test against it. 'One row per order' is vague. 'One
- Surrogate Keys (concepts: dmSurrogateKeys)
Surrogate keys are system-generated identifiers that replace natural keys in dimensional models. Every dimension row gets its own surrogate key (customer_sk, product_sk, date_sk). Fact tables reference dimensions using these surrogate keys, not the natural business keys. Why Not Just Use Natural Keys? Natural keys (email, SKU, employee_id) come from source systems. They have three problems in analytical models: they change (email updates), they get reused (SKU recycled for a new product), and th