Aller au contenu

What is the difference between a fact table and a dimension table?

par Équipe NextData

0

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

0

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, amount
  • dim_product: product_key, name, category, brand
  • dim_store: store_key, city, region
  • dim_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.

Équipe NextData

Signaler

Votre réponse

Connectez-vous pour répondre