What is the difference between a fact table and a dimension table?
par Équipe NextData
I am designing my first data warehouse and keep reading about fact tables and dimension tables in a star schema. How do I decide which data goes where?
Signaler
1 réponse
A fact table records events you want to measure. A dimension table describes the context of those events.
| Fact table | Dimension table | |
|---|---|---|
| Contains | Numeric measures and foreign keys | Descriptive attributes |
| One row is | One event: a sale, a click, a payment | One entity: a customer, a product, a date |
| Size | Very many rows, few columns | Fewer rows, many columns |
| Used to | Sum, count, average | Filter, group, label |
Example for retail sales:
fact_sales:date_key,product_key,store_key,quantity,amountdim_product:product_key,name,category,branddim_store:store_key,city,regiondim_date:date_key,date,month,quarter,year
A typical query sums a measure from the fact table, grouped by attributes of the dimensions:
SELECT d.year, p.category, SUM(f.amount) AS revenue
FROM fact_sales AS f
JOIN dim_date AS d ON d.date_key = f.date_key
JOIN dim_product AS p ON p.product_key = f.product_key
GROUP BY d.year, p.category;
A quick test: if you would add it up, it is a measure and belongs in the fact table. If you would filter or group by it, it is an attribute and belongs in a dimension.
Before building, state the grain of the fact table in one sentence ("one row per order line"). Most modelling mistakes come from mixing grains in the same table.
Réponse rédigée par l'équipe NextData et relue par un expert.
