DWH Interview Question 36 - In what order do you load dimensions and facts, and why?

Data Warehouse Interview Q&A · Question 36

In this post we are going to cover load order: in what order do we load dimensions and facts, and why does it matter?

The short answer is dimensions before facts. A fact row does not store the product name or the customer ID from the source. It stores a surrogate key that the fact load looks up from the dimension. If the dimension row is not there yet, the lookup finds nothing, and the fact has nothing to point to.

We will run a fact load in the wrong order and see what happens, then run the same load in the right order, and finish with a simple row-count check that protects us from this mistake.

Question 36 of the Real DWH Question and Answers series on TechBrothersIT.

The interview answer

Say this first

Load dimensions before facts. Facts store surrogate keys that are looked up from the dimensions, so a dimension row must exist before any fact can point to it. The full order is: staging, then dimensions (parents first), then bridges, then facts, then aggregates. Independent dimensions, and independent facts, can load in parallel.

The load order, end to end

Here is the full pipeline, layer by layer. Each layer only reads from the layers before it.

  • Staging - raw extracts from the source systems. Nothing in the warehouse depends on anything yet.
  • Dimensions - these create the surrogate keys.
  • Bridges - bridge tables need the dimension keys.
  • Facts - the fact load looks up all of those keys.
  • Aggregates - aggregates and snapshots are built from the facts, so they come last.

Let's create some sample data

We need two staging tables with today's extracts, plus an empty product dimension and an empty fact table in the warehouse.

Sample data — run this first
DROP TABLE IF EXISTS fact_sales;
DROP TABLE IF EXISTS dim_product;
DROP TABLE IF EXISTS stg_sales;
DROP TABLE IF EXISTS stg_product;

-- staging: today's extracts
CREATE TABLE stg_product (product_id VARCHAR(10), name VARCHAR(50));
INSERT INTO stg_product VALUES ('P10', 'Pen'), ('P20', 'Notebook');
CREATE TABLE stg_sales (sale_id INT, product_id VARCHAR(10), amount DECIMAL(10,2));
INSERT INTO stg_sales VALUES (1, 'P10', 5.00), (2, 'P20', 12.00), (3, 'P10', 5.00);

-- warehouse tables, still empty
CREATE TABLE dim_product (product_key INT PRIMARY KEY, product_id VARCHAR(10), name VARCHAR(50));
CREATE TABLE fact_sales (sale_id INT PRIMARY KEY, product_key INT, amount DECIMAL(10,2));

Here are the products in staging.

Query
SELECT * FROM stg_product ORDER BY 1;
Result2 rows · 2 columns
product_idname
P10Pen
P20Notebook

Two products, P10 and P20. And the sales in staging.

Query
SELECT * FROM stg_sales ORDER BY 1;
Result3 rows · 3 columns
sale_idproduct_idamount
1P105.00
2P2012.00
3P105.00

Three sales, two for P10 and one for P20. Both dim_product and fact_sales are still empty.

Step 1: The wrong order - facts first

Let's see what happens if the fact load runs before the dimension. The load joins staging to dim_product on product_id to get each product key.

Statement
INSERT INTO fact_sales
SELECT s.sale_id, d.product_key, s.amount
FROM stg_sales s
JOIN dim_product d
  ON d.product_id = s.product_id;

Now let's check the result.

Query
SELECT COUNT(*) AS fact_rows FROM fact_sales;
Result1 row · 1 column
fact_rows
0

Zero rows. The dimension is empty, so every lookup failed and every sale was dropped by the inner join.

And notice that there is no error. The insert succeeds, the job reports success, and the dashboard shows zero sales. That is what makes the wrong order dangerous.

Step 2: The right order - load the dimension

Now let's do it properly. First we load the product dimension, giving each product its surrogate key.

Statement
INSERT INTO dim_product
SELECT ROW_NUMBER() OVER (ORDER BY product_id),
       product_id, name
FROM stg_product;

Now let's check the result.

Query
SELECT * FROM dim_product ORDER BY product_key;
Result2 rows · 3 columns
product_keyproduct_idname
1P10Pen
2P20Notebook

Pen (P10) gets key 1 and Notebook (P20) gets key 2. Now the keys exist for the facts to look up.

Step 3: Load the fact

Now we run the exact same fact load as in Step 1.

Statement
INSERT INTO fact_sales
SELECT s.sale_id, d.product_key, s.amount
FROM stg_sales s
JOIN dim_product d
  ON d.product_id = s.product_id;

Now let's check the result.

Query
SELECT * FROM fact_sales ORDER BY sale_id;
Result3 rows · 3 columns
sale_idproduct_keyamount
115.00
2212.00
315.00

This time every lookup succeeds. All three sales are loaded: sales 1 and 3 point to product key 1, and sale 2 points to product key 2. Same code, different order, completely different result.

Order among dimensions, and running in parallel

Order matters inside the dimension layer too.

  • Outriggers first - a dimension that points to another one loads after it. An outrigger like a geography table loads before the customer dimension that references it.
  • Snowflake parents first - in a snowflaked dimension, parent tables load before their children.
  • Independent tables in parallel - dimensions with no dependencies on each other, like product and store, can load at the same time. The same goes for facts that do not depend on each other, like sales and returns.

Orchestration tools model this as a dependency graph and run everything that is ready at the same time.

Step 4: Check that staged equals loaded

Finally, let's protect ourselves with a check. After every fact load, we compare the number of rows in staging with the number of rows loaded.

Query
SELECT (SELECT COUNT(*) FROM stg_sales)  AS staged,
       (SELECT COUNT(*) FROM fact_sales) AS loaded;
Result1 row · 2 columns
stagedloaded
33

Three staged and three loaded, so nothing was dropped by the key lookup. If we had run this check after Step 1, it would have shown 3 staged and 0 loaded, and caught the problem straight away.

Tip

If staged and loaded do not match, either fail the job or route the missing rows to an unknown member or an inferred member, as covered in questions 16 and 30.

That way a wrong load order can never silently empty a report.

Follow-up questions interviewers ask

What if a fact arrives before its dimension row?

Use an inferred member (or the unknown member) so the fact is not dropped; see the late-arriving dimensions video.

Can dimensions load in parallel?

Yes, if they do not depend on each other; dependent ones (outriggers, snowflake parents) load first.

Why is the wrong order dangerous?

An inner-join key lookup silently drops facts: the job succeeds with zero or missing rows.

The full script

Here is everything from this post in one script. Copy it and run it top to bottom.

Full script — copy and run top to bottom
-- ========================================================================
-- Data Warehouse Interview Q&A #36 - Load order: dimensions before facts
-- TechBrothersIT. Sample data is invented for teaching.
-- Standard SQL, tested on PostgreSQL 16. Runs top to bottom in one go.
-- Run it in an empty database or schema: other videos reuse table names
-- like dim_customer, and their leftover tables can block the DROPs below.
-- PostgreSQL example:  CREATE SCHEMA dwh_q36;  SET search_path TO dwh_q36;
-- ========================================================================

-- Step 0: create and load the sample data
DROP TABLE IF EXISTS fact_sales;
DROP TABLE IF EXISTS dim_product;
DROP TABLE IF EXISTS stg_sales;
DROP TABLE IF EXISTS stg_product;

-- staging: today's extracts
CREATE TABLE stg_product (product_id VARCHAR(10), name VARCHAR(50));
INSERT INTO stg_product VALUES ('P10', 'Pen'), ('P20', 'Notebook');
CREATE TABLE stg_sales (sale_id INT, product_id VARCHAR(10), amount DECIMAL(10,2));
INSERT INTO stg_sales VALUES (1, 'P10', 5.00), (2, 'P20', 12.00), (3, 'P10', 5.00);

-- warehouse tables, still empty
CREATE TABLE dim_product (product_key INT PRIMARY KEY, product_id VARCHAR(10), name VARCHAR(50));
CREATE TABLE fact_sales (sale_id INT PRIMARY KEY, product_key INT, amount DECIMAL(10,2));


-- Step 1: Wrong order: load the fact before the dimension
INSERT INTO fact_sales
SELECT s.sale_id, d.product_key, s.amount
FROM stg_sales s
JOIN dim_product d
  ON d.product_id = s.product_id;

-- check the result
SELECT COUNT(*) AS fact_rows FROM fact_sales;

-- Step 2: Right order, step 1: load the dimension
INSERT INTO dim_product
SELECT ROW_NUMBER() OVER (ORDER BY product_id),
       product_id, name
FROM stg_product;

-- check the result
SELECT * FROM dim_product ORDER BY product_key;

-- Step 3: Right order, step 2: load the fact (same statement as before)
INSERT INTO fact_sales
SELECT s.sale_id, d.product_key, s.amount
FROM stg_sales s
JOIN dim_product d
  ON d.product_id = s.product_id;

-- check the result
SELECT * FROM fact_sales ORDER BY sale_id;

-- Step 4: Check: every staged sale reached the fact table
SELECT (SELECT COUNT(*) FROM stg_sales)  AS staged,
       (SELECT COUNT(*) FROM fact_sales) AS loaded;

Key points

  • Dimensions before facts: facts need their keys
  • Staging → dims → bridges → facts → aggregates
  • Parents first: outriggers, snowflake parents
  • Independent tables load in parallel
  • Check staged = loaded after every fact load
Next in this series

Question 37 — Full vs incremental load and CDC Full load vs incremental load: what is CDC?

Every query and every result on this page was run on PostgreSQL 16 using the script above. The SQL is standard and runs on most databases with small changes (noted in the script). All tables and data are invented demo data for teaching.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.