Sub topic 14
Why a star schema is smaller and faster
This chapter assumes you can load data in Power BI Desktop, read the Model view, and write simple DAX. You should also know what a fact table and a dimension table are.
How VertiPaq stores data
Power BI's engine, VertiPaq, stores each column separately and compresses it. Compression works best when a column has few distinct values. A column with 20 distinct values compresses far better than one with 2 million, even if both have the same row count.
Model size and speed therefore depend mostly on column cardinality, which is the number of distinct values in a column. Row count matters less.
Where a flat table wastes space
A single wide sales table repeats product name, category, customer city, and similar text on every order line. The engine must store and scan those columns at fact-table row count.
A star schema moves those attributes into dimensions. The fact table keeps only integer keys and numeric columns. The text is stored once per product or customer, not once per sale.
| Design | Text attributes stored | Fact table holds |
|---|---|---|
| Flat table | On every sales row | Keys, text, and numbers mixed |
| Star schema | Once per dimension row | Integer keys and numbers |
Why queries run faster
A visual that shows sales by category follows these steps:
- The category value filters the small product dimension.
- The relationship passes the matching product keys to the fact table.
- The engine sums the numeric column over those rows only.
The engine scans a short dimension first and then aggregates a narrow fact column. In a flat table, it would scan and group a long text column across every fact row.
Habits that keep the model small
- Use integer keys for relationships. Integers compress and join better than long strings.
- Remove columns you never filter, group, or aggregate by. Hidden columns still use memory.
- Avoid high-cardinality columns in the fact table, such as full timestamps. Split them into a date key and a time key.
- Keep the date dimension at day grain and relate it by a whole-number key.
- Do not store calculated columns in the fact table when a measure can do the same job. Calculated columns are stored. Measures are computed only when a query needs them.
The example below splits a timestamp into a date key and a time-of-day column in Power Query. It assumes a query named SalesFlat with an OrderTimestamp column of type datetime.
let
Source = SalesFlat,
AddDateKey = Table.AddColumn(
Source,
"DateKey",
each Date.Year([OrderTimestamp]) * 10000
+ Date.Month([OrderTimestamp]) * 100
+ Date.Day([OrderTimestamp]),
Int64.Type
),
AddTime = Table.AddColumn(
AddDateKey,
"OrderTime",
each Time.From([OrderTimestamp]),
type time
),
Result = Table.RemoveColumns(AddTime, {"OrderTimestamp"})
in
ResultThe new DateKey column has at most a few thousand distinct values. The original timestamp could have millions.
Where the star schema does not help
A star schema does not fix a slow measure. It also does not help if you keep the long text columns in the fact table. The benefit comes from the structure and from removing wasteful columns. Splitting tables alone does not deliver it.
Exercise
A fact table has 50 million rows and a ProductName text column with 3,000 distinct values. Why does moving ProductName into DimProduct usually shrink the model and speed up queries?
Show answer
The fact table then stores only a ProductKey integer instead of repeating the text on every row. Product names are stored once per product, 3,000 times in total. Filters on product name hit the small dimension first and pass keys to the fact table, so the engine does not scan a wide text column across 50 million rows.