Data Warehouses and OLAP
Systems built for analytics over huge datasets: OLTP vs OLAP, columnar storage, ETL vs ELT, lakes, lakehouses and star schemas.
What is it?
The database behind your app answers questions like "get order 123" or "insert this payment" thousands of times a second. That's OLTP (Online Transaction Processing): many small reads and writes touching a few rows each.
Analysts and dashboards ask different questions: "revenue by country by week for the last two years", "which features do retained users use?". These scan billions of rows but only a few columns, and run far less often. That's OLAP (Online Analytical Processing).
Running OLAP queries on your OLTP database is slow and dangerous: one big report can starve production traffic. So companies copy data into a data warehouse (Snowflake, BigQuery, Redshift, ClickHouse and others) designed for analytics: columnar storage, heavy compression, and massively parallel scans.
Around the warehouse you'll hear about: - ETL vs ELT: transform data before loading it, or load raw data and transform it inside the warehouse. - Data lakes: cheap object storage holding raw files of any shape. - Lakehouses: lakes plus table formats (Delta Lake, Apache Iceberg, Apache Hudi) that add transactions and schemas. - Star schema: the classic way to model analytics data as facts and dimensions.
Explain like I'm 10
OLTP is a shop's till: fast, one sale at a time. OLAP is the accountant at year's end with every receipt, adding up columns. You wouldn't let the accountant work at the till during the lunch rush, so you give them a photocopy of the receipts in their own office, sorted the way they like.
Examples
Row vs columnar layout
Table: orders(order_id, date, country, amount)
Row store (OLTP): each row's values stored together
[1, 2026-11-01, IN, 40] [2, 2026-11-01, US, 25] [3, 2026-11-02, IN, 60]
Column store (OLAP): each column's values stored together
order_id: [1, 2, 3]
date: [2026-11-01, 2026-11-01, 2026-11-02]
country: [IN, US, IN]
amount: [40, 25, 60]
SELECT country, SUM(amount) FROM orders GROUP BY country;
-> column store reads only 'country' and 'amount', skipping everything elseWith 100 columns, a query touching 2 reads about 2% of the data in a column store. Similar values side by side also compress far better.
A star schema and a typical query
-- Fact table: one row per event, mostly foreign keys and numbers
CREATE TABLE fact_sales (
date_key INT, -- -> dim_date
product_key INT, -- -> dim_product
customer_key INT, -- -> dim_customer
quantity INT,
revenue DECIMAL(12, 2)
)
PARTITION BY date_key; -- syntax varies by warehouse
-- Dimension tables: descriptive attributes, wide and comparatively small
-- dim_date(date_key, date, week, month, quarter, year, is_holiday)
-- dim_product(product_key, name, category, brand)
-- dim_customer(customer_key, country, segment, signup_date)
SELECT d.month, p.category, SUM(f.revenue) AS revenue
FROM fact_sales f
JOIN dim_date d ON f.date_key = d.date_key
JOIN dim_product p ON f.product_key = p.product_key
WHERE d.year = 2026
GROUP BY d.month, p.category;The date filter lets the engine skip whole partitions (partition pruning), and only a handful of columns are scanned.
ELT with SQL models (dbt style)
-- models/daily_revenue.sql : raw data is already loaded; transform in the warehouse
SELECT
DATE(created_at) AS order_date,
country,
SUM(amount_cents) / 100 AS revenue
FROM {{ ref('raw_orders') }}
WHERE status = 'paid'
GROUP BY 1, 2In ELT, transformations are versioned SQL run by the warehouse's own compute. Raw data stays available, so you can rebuild models when logic changes.
How it works
Why columnar is fast for analytics. - Less I/O: read only the columns a query uses. - Compression: a column holds one type with many repeats, so dictionary, run-length and delta encoding often shrink data several-fold or more, meaning even less I/O. - Vectorized execution: process batches of column values in tight loops that use CPU caches and SIMD well. - Zone maps / min-max stats: per-block min and max values let the engine skip blocks that can't match a filter. - The cost: single-row inserts and updates are expensive (a row's values are spread over many column files), so warehouses prefer bulk loads and append-heavy workloads.
Massively parallel processing (MPP). Data is split across many nodes or files; each scans its part, and partial aggregates are combined. Many modern warehouses separate storage and compute: data sits in object storage, and compute clusters scale (or pause) independently.
Partitioning and clustering. Most analytics filters on time, so tables are partitioned by date. A query for "last 7 days" reads 7 partitions instead of years. Clustering/sort keys inside partitions (e.g. by customer_id) make other filters prune well too. Too many tiny partitions hurt: small files mean overhead per file.
Getting data in. - ETL (Extract, Transform, Load): clean and reshape in a separate system, then load. Traditional when warehouse compute was expensive; still useful for removing sensitive fields before data lands. - ELT: load raw data first, transform with SQL inside the warehouse. Common today because warehouse compute is elastic and raw data stays available for reprocessing. - CDC (change data capture) streams row changes from the OLTP database's log into the warehouse, avoiding heavy nightly extracts.
Lake and lakehouse. A data lake stores raw files (JSON, CSV, Parquet) in object storage cheaply, with schema applied on read. Without discipline it becomes a "data swamp". Open table formats (Iceberg, Delta Lake, Hudi) add a metadata layer over Parquet files: ACID commits, schema evolution, time travel and efficient upserts, so many engines (Spark, Trino, Flink, warehouses) can share one copy of the data. That combination is called a lakehouse.
Modelling. A star schema has a central fact table (events or transactions with numeric measures) surrounded by dimension tables (who, what, where, when). A snowflake schema normalises dimensions further. Dimensions that change over time (a customer moves country) use slowly changing dimension techniques, for example Type 2: add a new row with validity dates so history stays correct.
OLTP DBs --CDC/extract--> [ Object storage: raw zone ] --transform--> [ curated tables ]
SaaS APIs --------------> [ (data lake, Parquet) ] (Iceberg/Delta or
Event stream -----------> [ ] warehouse tables)
|
BI dashboards, ad-hoc SQL, ML
Star schema
dim_date
|
dim_product -- fact_sales -- dim_customer
|
dim_storeWhy does it exist?
Transactional databases are optimised to touch a few rows quickly and safely. Analytical questions touch most rows but few columns, and combine data from many systems. Using one engine for both makes each worse. A separate analytics store lets the business query everything it knows without endangering production.
When to use it
- Business reporting, dashboards and ad-hoc analysis over months or years of data. - Combining data from many sources (app databases, payments provider, CRM, events) into one place. - Feature generation and training data for ML. - High-volume event analytics (ClickHouse, Druid, Pinot are popular for real-time, user-facing analytics).
When not to use it
- Serving your app's transactional reads and writes: warehouses have higher per-query latency and poor single-row update performance. - Small data. If everything fits in one Postgres instance and reports run in seconds, a read replica may be all you need. - Strictly real-time decisions in the request path. Most warehouse pipelines have minutes-to-hours of lag; use a stream processor or an OLTP store for that.
Common mistakes
Running heavy analytics queries directly on the production primary database.
Using SELECT * in a columnar warehouse, which throws away the main benefit and inflates scan costs.
Not partitioning by date (or filtering on the partition column), so every query scans all history.
Producing millions of tiny files in a lake, which kills query planning and scan performance; compact them.
Overwriting dimension attributes in place, so historical reports silently change (e.g. last year's sales move to a customer's new country).
Practice exercises
- Easy:
Explain why a columnar database is faster than a row database for 'average order value per country' but slower for 'update order 42'.
- Medium:
Design a star schema for a ride-hailing company's trip analytics. Name the fact table, its grain, the measures and at least four dimensions.
- Hard:
Design a pipeline that keeps a warehouse within 5 minutes of a 2 TB Postgres OLTP database, handles schema changes, supports reprocessing after a bug in transformation logic, and removes personal data for deleted users.
Interview questions
What's the difference between OLTP and OLAP?
OLTP handles many small, concurrent transactions touching few rows, with low latency. OLAP runs fewer, large queries that scan and aggregate many rows across few columns.
Why is columnar storage good for analytics?
Queries read only the columns they need, columns of one type compress very well, and engines can process column batches with vectorized execution. Together this cuts I/O and CPU per row dramatically.
What is a star schema?
A central fact table of measurable events (sales, trips) with foreign keys to surrounding dimension tables that describe them (date, product, customer). It's simple to query and works well with columnar engines.
ETL vs ELT?
ETL transforms data before loading it into the warehouse; ELT loads raw data first and transforms it inside the warehouse with SQL. ELT is common now because warehouse compute is elastic and keeping raw data enables reprocessing.
What is partition pruning?
When a query filters on the partition column (usually date), the engine skips partitions that can't match, reading only relevant data. Without that filter, it scans everything.
Data lake vs data warehouse?
A lake stores raw files of any format cheaply in object storage, with schema on read. A warehouse stores structured, modelled tables optimized for SQL, with schema on write. Lakes are flexible and cheap; warehouses are easier to query reliably.
What is a lakehouse?
Lake storage (Parquet on object storage) plus an open table format like Iceberg, Delta Lake or Hudi that adds ACID transactions, schema evolution and time travel, so warehouse-style workloads can run directly on the lake with many engines.
How do you keep a warehouse in sync with an OLTP database without hurting it?
Use change data capture from the database's replication log (for example Debezium into Kafka), apply changes as merges in the warehouse, and avoid large repeated full-table extracts against the primary.
What is a slowly changing dimension Type 2?
When a dimension attribute changes, you insert a new row with a new surrogate key and validity dates instead of overwriting. Facts point to the version valid at the time, so historical reports stay accurate.
Your warehouse bill doubled and dashboards got slower. How do you investigate?
Find the most expensive queries by bytes scanned or credits used. Look for full scans missing partition filters, SELECT *, poorly clustered tables, repeated recomputation that could be materialized, and tiny-file problems. Add partition/cluster keys, incremental models and aggregate tables, and set per-team quotas.