internalsGet Pro

Module 11 · Lesson 1 of 11Free

Data warehouses: built for analytical queries

Level
Beginner
Topic
Data warehouses
Reading time
13 min

When you finish this page you can

  1. Explain why a long analytical query hurts an app’s primary database and gets cancelled on its read replica.
  2. Name the five things a data warehouse is built for, and how Snowflake does each one.
  3. Estimate how much data a year-long revenue query reads in a row store and in a columnar warehouse.
  4. Choose where a question should run, by how much data it reads and how fresh it must be.
  1. Operational vs analytical data: two different jobs13 min
  2. ETL vs ELT: where the transformation happens13 min
0:00 / 6:31
Show transcript

0:00 It’s late December 2027. An analyst on Daniel’s finance team at Kartly, our example online shop, needs this year’s revenue, month by month, to close the books. Snowflake’s numbers are only as of last night, they think. The orders database must be better. So they run the query on its read replica. Four minutes later, the database cancels it. They run it again. Cancelled again. So they ask Meera, Kartly’s data engineer, for access to the main orders database instead. Kartly takes about 500,000 orders a day. They live in a PostgreSQL database that checkout writes to all day long. A copy, loaded and reshaped, sits in Snowflake. Meera says no to the main database. Why? What is a data warehouse built to do, that the orders database is not?

0:46 The orders database is built for one kind of work: lots of tiny reads and writes, each touching a few rows. Checkout saving one order is a good example. That kind of work is called OLTP. Finance asks something different. Read a big part of the data, and add it up. That kind is called OLAP. Start with the replica. A read replica is a copy of the database that keeps applying every change the main one makes. This is called replay. Kartly’s orders change about 2.5 million times a day. Some of those changes clean away old versions of rows. If a long query still needs those versions, replay has to wait. By default, it waits 30 seconds. Then PostgreSQL picks replay over the query, and cancels the query. So a query that writes nothing can still be in the way. There is a setting that stops these cancels. But it moves the same cleanup problem onto the main database. So why not the main database, the primary? There, the query fights checkout for disk and processor time. And while it runs, it holds an old snapshot of the data. PostgreSQL’s cleanup, called vacuum, can’t remove old row versions while that snapshot is open. They pile up as wasted space in Kartly’s busiest table.

1:53 How much data does this question actually read? In 2027 so far, Kartly has taken about 150 million orders. PostgreSQL is a row store. It keeps each order’s columns together, so it reads whole rows, every column. For this year’s orders, that’s about 30 gigabytes. And if those rows are spread through the table, it might read the whole thing, up to 200 gigabytes. Say the disk reads 500 megabytes a second. Then the smaller read takes about a minute. The whole table takes about seven minutes. Either way, that’s far longer than the half minute replay will wait. But the question only needs three columns: the date, the amount, and whether the order counts as revenue. That’s about 20 bytes an order, or about 3 gigabytes in all. Roughly ten times less than the smaller row read. A columnar store keeps each column together, so it reads just those three. A bigger replica doesn’t fix this. It still reads whole rows, on one machine. And it only holds the orders database. Payments, partner sales and clicks all live somewhere else.

2:55 So what is a data warehouse? It’s a database built for questions about a company’s history. It’s loaded from many sources, and kept apart from the databases the apps run on. Kartly’s warehouse is Snowflake, and it does five things the orders database can’t. One: read a few columns of many rows. Snowflake stores tables column by column, compressed, and scans only the columns a query names. Two: skip data a query can’t need. Every small chunk of a table remembers the range of values in each column. Orders arrive in date order, so a 2027 query never opens chunks from 2024. Three: split one query across many machines. The query is cut into pieces, and every machine works on its own piece at the same time. This is called massively parallel processing, or MPP. Four: many teams at once. In Snowflake, each team’s compute is a virtual warehouse, and it shares nothing with the others. Five: data from every source, with its history. Raw data is loaded in first, then reshaped into tables built for analysis. Here’s Kartly’s Snowflake. One copy of the data, and four separate warehouses. One loads new orders. One runs the nightly rebuild. One serves the dashboards. And one runs analysts’ questions, including finance’s. Finance’s query reads three columns, skips everything before 2027, and splits the rest across its machines. The others keep working. Checkout never notices.

4:16 A warehouse isn’t just a faster copy of the orders database. It gives three things up. First, freshness. The analysis tables are rebuilt each night, and ready by seven in the morning. Raw orders are merged in every 15 minutes. Nothing in Snowflake is up to the second. Second, small, fast writes. Checkout updates one order at a time, thousands of times a minute. A warehouse is built for big bulk loads instead. Third, cost per question. Compute is billed in credits while a warehouse runs. The smallest size costs 1 credit an hour, and every start is billed for at least 60 seconds. So the analyst’s worry was half right. The finance tables really are as of last night. But a year-end close covers days that are already over. For that, last night is enough.

4:59 So where should finance’s year-long questions run? On the primary, they’re up to the second, but they take from checkout. On the existing replica, they get cancelled. A separate replica just for reports could wait for long queries. But it still reads whole rows on one machine, from one database, with no history. The warehouse reads only what it needs, from every source. The answer changes with size, and with the question. When Kartly took 60,000 orders a day from one source, a nightly DuckDB file was enough. A question about one order, right now, belongs on the orders database. And a dashboard that must be only seconds behind needs a real-time engine. You can try this on a laptop. The lesson’s script builds 20 million made-up orders in DuckDB, a columnar engine. Adding up the whole year touches every row, but only the amount column. Now picture 1.5 billion order lines, many sources, and dozens of people asking at once. That’s where one machine stops being enough.

5:54 To recap. One. A warehouse is built for questions over a company’s history: few columns, skipped data, many machines, separate teams, every source. Two. On a busy replica, a long query is cancelled after 30 seconds of blocked replay. Three. Reading three columns instead of whole rows reads about ten times less. Four. The warehouse gives up freshness, small writes and free questions. Five. One order now goes to the orders database. History goes to the warehouse. Kartly’s map showed one copy of the data and four warehouses. How can four warehouses read the same tables without copying them? That’s the next lesson: how cloud warehouses separate storage and compute.

A year-end query, cancelled after four minutes

It’s Tuesday, 28 December 2027, 11:00 IST. An analyst on Daniel’s finance team wants 2027’s revenue by month for the year-end close. Snowflake’s numbers are “as of last night”, the analyst thinks, so the orders database must be better. They run the query on its read replica.

Four minutes later, PostgreSQL cancels it. They run it again. It is cancelled again. So they ask Meera, Kartly’s data engineer, for access to the primary database instead.

is an online store in India. It takes about 500,000 orders a day. The orders live in a PostgreSQL database that checkout writes to all day. A copy of them, loaded and reshaped, lives in Snowflake, in KARTLY.ANALYTICS. Daniel, who leads finance reporting, reads his numbers from there.

Meera says no to the primary. This lesson explains why: what is a built to do that the orders database isn’t?

Check yourselfAnswer in your head first, then reveal
The query only reads orders. It writes nothing. Why would PostgreSQL cancel it on a replica?Show answerHide answer
Because the replica must keep applying the primary’s changes, and the long query blocks some of them. After a time limit, PostgreSQL picks replay over the query and cancels it. A read-only query can still be in the way.
The same query in three places. Only the warehouse runs it without taking something from the store.

Why the orders database can’t answer it

The orders database is built for : many small reads and writes, each touching a few rows. Finance’s question is : read a large part of the data and add it up. OLTP vs OLAP covers why the two workloads differ. Here is what that difference costs at Kartly.

On the replica, the query is cancelled. A replica replays every change the primary makes. When a cleanup change would remove row versions a running query still needs, replay waits. After max_standby_streaming_delay, 30 seconds (default of max_standby_streaming_delay), the query is cancelled 2. This is a . PostgreSQL’s docs warn that heavily updated tables quickly cancel long queries on a replica 2. Kartly’s orders change about 2.5 million times a day. Reading and writing databases from Python shows this conflict step by step.

On the primary, the query takes from checkout. It competes with checkout for disk reads and processor time. It also holds an old snapshot for minutes. While it runs, can’t remove the old row versions that those 2.5 million daily changes leave behind. They pile up as bloat in the busiest table Kartly has.

Turning on on the replica stops the cancellations. But it moves the same bloat onto the primary 2.

How much does this query actually read?

Disk speed and bytes per column are assumptions for this estimate. The order counts and the 200-byte order row come from earlier lessons.

Orders in 2027 so far

about 150 million orders, fewer a day early in the year than today’s 500,000

About 15% of the roughly 1 billion orders since launch.

PostgreSQL reads whole rows

150 million × 200 bytes ≈ 30 GB, and up to 1 billion × 200 bytes ≈ 200 GB

A row store keeps each order’s columns together, so it reads all of them. If 2027’s rows are spread through the table, it may read the whole table.

At an assumed 500 MB a second

30 GB ÷ 0.5 GB/s ≈ 60 s; 200 GB ÷ 0.5 GB/s ≈ 400 s, about 7 minutes

Either way, far longer than the 30 seconds replay will wait.

The columns the question needs

150 million × about 20 bytes (date, amount, revenue flag) ≈ 3 GB

About a tenth of the smallest row-store read, before any compression.

The same question reads about ten times less data when only its three columns are stored together.

A bigger replica doesn’t change this. It still reads whole rows, on one machine, and it still holds only the orders database. Payments, partner sales and clicks live elsewhere.

What a warehouse is built for

A data warehouse is a database built for analytical queries over a company’s history. It is loaded from many sources and kept apart from the databases the apps run on. is Kartly’s. It is built for five things the orders database isn’t.

Built for How Snowflake does it Covered in
Reading a few columns of many rows Stores tables in a compressed, columnar format 1; only the columns a query names are scanned 3 Row and columnar storage
Skipping data a query can’t need Keeps the range of values of each column in each small chunk of a table 3, so a 2027 filter skips older chunks Partitioning, clustering and pruning
Splitting one query across many machines Runs queries on clusters where each node holds part of the data 1 Columnar execution and MPP
Many teams at once Each is its own compute and shares nothing with the others 1 How cloud warehouses separate storage and compute
Data from every source, with history Loads raw data into RAW; dbt models it into ANALYTICS Operational vs analytical data

The third row is called (MPP): one query is cut into pieces, and many machines each work on their own piece at the same time.

The skipping in the second row is a form of . Orders arrive in date order, so most chunks hold one narrow range of dates. A 2027 query never opens chunks from 2024.

One copy of the data, four separate pools of compute. Finance’s query runs on ADHOC_WH and slows none of the others.

Finance’s query runs on ADHOC_WH and reads fct_orders in ANALYTICS. That table holds about 1 billion orders. The query reads three columns, skips the chunks before 2027 and splits the rest across the warehouse’s machines. Meanwhile, LOAD_WH keeps merging new orders and Metabase dashboards keep loading on BI_WH. Checkout never sees any of it.

What the warehouse gives up

A warehouse isn’t a faster copy of the orders database. It trades three things away.

  • Freshness. ANALYTICS is rebuilt each night and ready by 07:00. RAW.ORDERS_CURRENT is merged every 15 minutes from change data capture (Landing CDC data in a warehouse or lakehouse). Nothing in Snowflake is up to the second.
  • Small, fast writes. Checkout updates one order at a time, thousands of times a minute. A warehouse is built for bulk loads, not that.
  • Cost per question. Compute is billed while a warehouse runs, in credits an hour by size: X-Small 1, doubling each size to 6X-Large 512. Each start is billed for at least 60 seconds each time it starts. Warehouse costs covers the bill.

So the analyst’s worry was half right. Snowflake’s ANALYTICS is as of last night. For a year-end close that ends before today, last night is enough.

Where should finance’s year-long questions run?

On the orders primary.

Up to the second. But the query competes with checkout for disk and processor time, and holds back cleanup of the busiest table for as long as it runs.

On the existing read replica.

Checkout is safe. But long queries are cancelled after 30 seconds of blocked replay, and fixing that either lets the replica fall behind or moves bloat to the primary.

On a separate replica just for analytics.

A replica set aside for reports can wait for long queries. But it still reads whole rows on one machine, holds only this one database, and keeps only current state.

Why this choice

Finance’s questions span hundreds of millions of rows and several sources. They need stable answers about the past, not the last second. That is what the warehouse is shaped for, and it keeps the load off the database customers use.

The choice would change with the size and the question. At stage 2, Kartly had 60,000 orders a day and one source, and a nightly DuckDB file was enough. A question about one order right now belongs on the orders database. A dashboard that must be seconds behind at 50,000 clicks a second needs a real-time engine, covered in Real-time OLAP.

Try it yourself

A columnar engine answers a year-long question fast, even on one laptop. You need Python 3.11 or later and pip install duckdb==2.0.0. Save this as year.py and run python year.py. It builds 20 million made-up orders in memory, so nothing is written to disk.

Python
import time

import duckdb

con = duckdb.connect()  # in memory

# 20 million made-up orders, spread evenly over the 365 days of 2027.
con.sql("""
    CREATE TABLE orders AS
    SELECT i AS order_id,
           DATE '2027-01-01' + CAST(i % 365 AS INTEGER) AS ordered_on,
           200 + i % 2001 AS amount_inr
    FROM range(20000000) AS t(i)
""")


def timed(label: str, sql: str) -> tuple:
    start = time.perf_counter()
    row = con.sql(sql).fetchone()
    print(f"{label} took {time.perf_counter() - start:.3f} s")
    return row


one = timed("one order", "SELECT * FROM orders WHERE order_id = 12345")
print(f"  order {one[0]}: {one[1]}, ₹{one[2]}")

year = timed("whole year", "SELECT count(*), sum(amount_inr) FROM orders")
print(f"  {year[0]:,} orders, ₹{year[1]:,}")

The output looks like this. Your times depend on your laptop, so they are shown as … here:

Text
one order took … s
  order 12345: 2027-10-28, ₹539
whole year took … s
  20,000,000 orders, ₹23,999,995,010

Look at the whole-year time. That query touches all 20 million rows, but only their amount_inr values; DuckDB never reads the other columns. Now picture 1.5 billion order lines, many sources and dozens of people querying at once. That is the point where one machine stops being enough.

Where this shows up

The map shows one copy of the data and four pools of compute. How can four warehouses read the same tables without copying them? That is the next lesson.

Common mistakes

Calling a read replica a warehouse

A replica is the same row store with the same limits. It also cancels long queries on busy tables. Give analysts a warehouse once their questions span millions of rows.

Asking the warehouse about right now

ANALYTICS is as of last night. For “today so far”, read RAW.ORDERS_CURRENT, which is about 15 minutes behind, and say so on the report.

Running every team on one warehouse

Loads, dbt builds and dashboards then queue behind each other. Give each kind of work its own virtual warehouse.

Check your understanding

Check yourselfAnswer in your head first, then reveal
The analyst raises statement_timeout to 2 hours on the replica and runs the query again. Does it finish?Show answerHide answer
Probably not. statement_timeout is the query’s own limit. The cancel comes from replay, which waits only 30 seconds for a conflicting query, however long the query’s own timeout is.
Check yourselfAnswer in your head first, then reveal
Support needs the status of order 48213907 while the customer waits. Should the support tool query Snowflake?Show answerHide answer
No. It is a lookup of one row’s current state, which the orders database does in milliseconds. Snowflake’s ANALYTICS may still show yesterday’s status, and if the warehouse is suspended, starting it for one row costs at least a minute of credits.
Check yourselfAnswer in your head first, then reveal
Finance’s query adds a category column from dim_product. In a columnar warehouse, roughly how does the data read change?Show answerHide answer
It grows by about one column’s worth, plus the small product table. Not by a whole row: the warehouse reads only the columns a query names.
Check yourselfAnswer in your head first, then reveal
During the 02:25 dbt build, Daniel opens a Metabase dashboard on BI_WH. Does the build slow it down?Show answerHide answer
No. The build runs on TRANSFORM_WH, and each virtual warehouse has its own compute. The dashboard may show yesterday’s numbers until the build finishes, but it isn’t slowed by it.

Summary

  1. A warehouse is built for analytical queries over a company’s history: it reads few columns of many rows, skips data it can’t need, splits work across machines, isolates teams and holds every source.
  2. On a PostgreSQL replica, a long query on a busy table is cancelled after 30 seconds of blocked replay; on the primary, it takes from checkout and holds back cleanup.
  3. Kartly’s 2027 revenue query reads an estimated 30–200 GB of whole rows in PostgreSQL, but about 3 GB of three columns in a columnar warehouse.
  4. The warehouse trades away freshness, small fast writes and free queries: ANALYTICS is as of last night and compute is billed while it runs.
  5. Questions about one order now go to the orders database; seconds-fresh dashboards go to a real-time engine; questions over history go to the warehouse.

Report a mistake

Tell us what’s wrong in this lesson and we’ll fix it.