Data Warehouse Concepts — Data Engineering

A data warehouse is an OLAP database kept apart from the app: OLTP serves operational transactions right now, OLAP analyses large amounts of historical data…

At stock-taking time, nobody counts the books on the shelves out front. The count happens in the storeroom, where the same goods are stacked by kind, so everything can be tallied at once without emptying the shelves out front. Out front, a clerk takes one book off the shelf for one customer: quick, one item. Same goods, different arrangement, because the question is different. Databases work the same way. The app's database is the shop floor, and a data warehouse is the storeroom: a database kept apart from the app, arranged specifically to answer questions about many rows and a long history.

OLTP and OLAP, once more

In the first lesson you met two kinds of database work. Here they are in full:

OLTP (Online Transaction Processing) OLAP (Online Analytical Processing)
Purpose operational transactions, right now analysing large amounts of historical data
At Bookshop checkout writes 1 order, the "My orders" page "which book sells best per city per week?"
Query shape many small queries, each touching a few rows a few big queries that read almost every row
The data the state right now: today's book price the history: every order since the shop opened
Tools MySQL, PostgreSQL (the app's database) BigQuery, Snowflake, Redshift, ClickHouse, DuckDB

A warehouse is an OLAP database. A pipeline copies its contents from the app's database (the ETL vs ELT lesson), so the owner's heavy queries never slow down checkout.

The sales table in the warehouse

This is the table Bookshop's pipeline built in the earlier lessons, from the raw copy raw_order_lines:

CREATE TABLE sales (
  order_id INT,
  book_id INT,
  order_date DATE,    -- the order's day
  week DATE,          -- the Monday of its week
  city VARCHAR(50),   -- 'Unknown' when empty
  title VARCHAR(200),
  qty INT,
  price INT           -- the catalogue price
);

Columnar: read only the columns you need

The app's database stores by row (row-oriented): all of a row's columns sit side by side on disk. A warehouse stores by column (columnar), like the Parquet file in the Data Formats lesson: all of one column's values sit side by side.

By row (MySQL, PostgreSQL, SQLite):
[1, 1, 2026-09-28, 2026-09-28, London, The Rainbow Troops, 1, 18]
[2, 2, 2026-09-28, 2026-09-28, Bristol, This Earth of Mankind, 1, 25]
...

By column (BigQuery, Snowflake, ClickHouse, DuckDB):
city: [London, Bristol, Bristol, Manchester, London, ...]
qty:  [1, 1, 1, 2, 2, ...]
...

sales has 18 rows and 8 columns, so 144 cells. Before you look at the table below, guess: stored by column, how many cells does SELECT city, SUM(qty) FROM sales GROUP BY city read, 144, 36 or 18? Here is what the two queries read:

Query By row By column
SELECT city, SUM(qty) FROM sales GROUP BY city 144 cells: every row is read whole 36 cells: only city and qty
SELECT * FROM sales WHERE order_id = 6 8 cells from 1 place (through an index) 8 cells from 8 places, one per column

The answer is 36: 2 columns times 18 rows. The result is Bristol 5, London 9, Manchester 5, Unknown 2. At 1 million rows, the Data Formats lesson measured GROUP BY city at 0.59 seconds on CSV and 0.009 seconds on Parquet. Besides reading less, columns compress well: city has only 4 distinct values, so each value is stored once (dictionary encoding).

The second row explains why apps keep row storage: reading or writing one order happens in one place. Columnar is fast for scans and slow for single rows.

Warehouse, data mart, data lake

What it holds At Bookshop
Data warehouse all of the company's analytical data, clean and organised, as one source of truth raw_order_lines, sales, later stock and ads too
Data mart a slice of the warehouse for one team or one question a weekly_best_sellers table for the owner's dashboard
Data lake raw files (Parquet, JSON Lines, CSV) in object storage such as S3, GCS or R2; the schema is read at query time one order_lines.parquet file per day

Inside the warehouse, tables usually come in layers: raw (the untouched copy) to clean to mart (ready to use), also called bronze, silver, gold. At Bookshop: raw_order_lines to sales to weekly_best_sellers. Data lakes and the lakehouse (a lake you can query like a warehouse) are covered in the Data Lake Architecture lesson.

Facts and dimensions: a first look

sales's columns come in two kinds:

In the Data Modelling (Star Schema) lesson, this wide table is split into one fact table fact_sales holding the numbers and keys, surrounded by the dimension tables dim_book, dim_date and dim_customer. For now, it's enough to tell a fact from a dimension.

💡 Data that changes slowly

The price column comes from the books catalogue when the table is built. On Saturday 10 October, This Earth of Mankind's price drops from 25 to 24, and that night sales is rebuilt in full from raw:

UPDATE books SET price = 24 WHERE id = 2;
-- then CREATE TABLE sales AS SELECT ... JOIN books b ... as usual
SELECT SUM(qty * price) AS revenue FROM sales;

SQLite answers 438, where it said 444 before. The 6 copies of This Earth of Mankind sold from 28 September to 9 October at $25 are now recorded at $24: last week's revenue changed although nothing happened last week. Descriptions that change slowly, such as a price, an address or a customer's city, are called a slowly changing dimension (SCD). Two common cures: keep the price at the time of the sale in the fact row and never recompute it, or keep every version of the price with valid_from and valid_to columns. The details are in the Data Modelling (Star Schema) lesson.

An idempotent daily load

The warehouse is filled by date, one part (partition) each night, with the pattern from the ETL vs ELT lesson:

BEGIN;
DELETE FROM sales WHERE order_date = '2026-10-01';
INSERT INTO sales
SELECT DISTINCT r.order_id, r.book_id,
       date(r.created_at) AS order_date,
       date(r.created_at, 'weekday 0', '-6 days') AS week,
       COALESCE(r.city, 'Unknown') AS city,
       r.title AS title,
       r.qty,
       b.price AS price
FROM raw_order_lines r
JOIN books b ON b.id = r.book_id
WHERE date(r.created_at) = '2026-10-01';
COMMIT;

Query patterns in a warehouse

Analytical questions are almost always one of these: summing per period (GROUP BY week), ranking within a group (RANK(), the SQL for Data Engineering lesson), or comparing with the previous period:

SELECT city, week, SUM(qty) AS books_sold,
       LAG(SUM(qty)) OVER (PARTITION BY city ORDER BY week) AS last_week
FROM sales
GROUP BY city, week
ORDER BY city, week;
city week books_sold last_week
Bristol 2026-09-28 3 NULL
Bristol 2026-10-05 2 3
London 2026-09-28 6 NULL
London 2026-10-05 3 6
Manchester 2026-09-28 3 NULL
Manchester 2026-10-05 2 3
Unknown 2026-09-28 1 NULL
Unknown 2026-10-05 1 1

One more habit: filter on the date as early as you can (WHERE order_date >= '2026-10-05'). A warehouse partitioned by date reads only the partitions that match, and in BigQuery that cuts the bill directly. That's the next lesson.

⚠️ Habits that make a warehouse slow or wrong

TL;DR: A data warehouse is an OLAP database kept apart from the app: OLTP serves operational transactions right now, OLAP analyses large amounts of historical data. A warehouse stores by column, so SELECT city, SUM(qty) reads only 36 of sales's 144 cells; row storage suits reading or writing a single order. A data mart is a slice of the warehouse for one team, a data lake is raw files in object storage. sales is denormalised with a grain of one title per order; its columns are facts (qty, price) and dimensions (order_date, city, title). Rebuilding it from a catalogue whose price changed rewrites history ($444 becomes $438 after This Earth of Mankind's price cut). Fill the warehouse by date with DELETE then INSERT in one transaction, and use the per-period, ranking and LAG patterns to compare periods.

⚡ OLTP versus OLAP

Meaning: OLTP is the app's database, for operational transactions right now, like a checkout writing 1 order. OLAP is the warehouse, for analysing large amounts of historical data, like the best seller per city per week.

⚡ One wide table

Write:

CREATE TABLE sales (
  order_id INT,
  book_id INT,
  order_date DATE,    -- the order's day
  week DATE,          -- the Monday of its week
  city VARCHAR(50),   -- 'Unknown' when empty
  title VARCHAR(200),
  qty INT,
  price INT           -- the catalogue price
);

Meaning: The grain is one row = one title in one order. city and title are copied into every row (denormalisation), so the owner's question needs no JOIN any more.

⚡ Columnar reads less

Guess first: sales has 144 cells. Stored by column, how many cells does SELECT city, SUM(qty) read?

The answer is 36: only the city and qty columns, 2 columns times 18 rows. Stored by row, all 144 cells are read.

Meaning: A warehouse stores by column, so name only the columns you need, not SELECT *.

⚡ A price that changes

Meaning: If sales is rebuilt from a catalogue whose price has dropped, last week's revenue changes too: $444 becomes $438. Keep the price at the time of the sale, or its history.

What you will learn