LESSON 06 · Analytics

Store analytics by column

Analytical queries read few columns across many rows, which calls for a different layout and often a separate system.

SYSTEM VIEW

The write path

Diagram view
Rows read—
Columns read—
Data read—

Watch one revenue report scan 1 billion rows in a row store, then in a column store, and weigh what a warehouse costs.

  1. The App does online transaction processing: many small reads and writes, each by key, at low latency. Fetching order 9001 reads one row from the Database, a row store that keeps all 60 of a row’s values together: 500 bytes.

    OLTP
    many small reads, writes
    Each
    by key, low latency

    What changed

    • Small reads and writes by key
    • One row: 60 values, 500 B
  2. Reports asks for revenue per region last quarter: online analytical processing. It needs 3 of the 60 columns, but nearly every one of 1 billion rows. The row store keeps each row’s values together, so the scan drags all 60 columns through memory: about 500 GB.

    Query
    revenue per region
    Needs
    3 of 60 columns
    Rows
    1 billion × 500 B

    What changed

    • Reports: revenue per region
    • Row store reads about 500 GB
  3. A column store keeps each column in its own files and reads only what the query names. Three columns of 8 bytes across 1 billion rows is 24 GB, against 500 GB for the row store. Snowflake, BigQuery, ClickHouse and Parquet store data by column.

    Reads
    3 columns × 8 B
    Row store
    500 GB
    Column store
    24 GB

    What changed

    • Column store
    • Reads 3 columns: 24 GB
  4. Values in one column look alike, so they compress well. The region column holds just 30 distinct values, so dictionary codes store each row’s region in under one byte: under 1 GB instead of 8 GB. The scan drops below 17 GB, and sorted by region the region column shrinks far more.

    Region
    30 distinct values
    Dictionary codes
    under 1 B per row
    Sorted by region
    far less

    What changed

    • Region column: 8 GB → under 1 GB
    • Scan under 17 GB
  5. The cost falls on writes and single-row reads. When Reports drills into order 9001, the whole row means opening all 60 column files, and inserting a row writes to every one of them. So column stores buffer new rows and merge them in batches.

    One row
    in 60 column files
    New rows
    buffered, merged in batches

    What changed

    • One row reads all 60 column files
    • New rows buffered, merged in batches
  6. Run as a separate warehouse, the column store still answers Reports under 17 GB. But a Pipeline must copy new rows from the Database in batches, so the data is minutes or hours behind, with one more system to run and another bill.

    Pipeline
    one more to run
    Data
    minutes or hours behind
    Cost
    another bill

    What changed

    • Pipeline
    • Column store becomes the Warehouse
    • Data minutes or hours behind
  7. Start by pointing Reports at a read replica: no pipeline, but the same 500 GB row scan. Choose Warehouse to see when one earns its keep: data from several systems, long history production shouldn’t carry, or scans too slow for the replica.

    Start with
    a read replica
    Warehouse for
    many sources, long history

    What changed

    • Read replica
    • Reports read the replica
    Reports read from

Space plays · ← → step · Home resets

THE IDEA

How it works

These engines have served online transaction processing (OLTP): many small reads and writes by key, at low latency. Online analytical processing (OLAP) is the opposite: fewer queries, each scanning millions of rows to aggregate a few columns. “Revenue per region last quarter” may touch 3 columns out of 60, but nearly every row in the quarter.

A row store keeps each row’s values together, so that scan drags every column through memory. A column store keeps each column in its own segments and reads only what the query names. As an example, 1 billion rows at 500 bytes each means a row store reads about 500 GB, while a column store reading 3 columns of 8 bytes reads 24 GB. Compression widens the gap, because values in one column look alike. A region column with 30 distinct values needs under one byte per row with dictionary codes, and far less if the table is sorted by region. Snowflake, BigQuery, ClickHouse, and the Parquet file format all store data by column.

The cost falls on writes and single-row reads. One row is spread across many column files, so inserting or fetching a whole row is slow, and column stores usually buffer new rows and merge them in batches.

A separate warehouse adds a pipeline to run, data minutes or hours behind, and another bill. Start by pointing reports at a read replica. A warehouse earns its keep when analytics needs data from several systems, long history that production should not carry, or scans too slow for a row-store replica.

DECISION POINT

Your 200 GB Postgres database has one read replica, where finance’s monthly report takes 40 minutes and bothers nobody. Now the product team wants a daily dashboard that joins orders with billing-provider exports and ad spend over five years of history. Do you add a warehouse?

Write a sentence or two to unlock the guidance. Committing first is what makes it stick.

Keep in mind

  • Column storage makes a scan cheap when a query reads few columns out of many.
  • Compression works well because values in one column look alike.
  • Try a read replica first; add a warehouse when analytics outgrows it.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
ApplyQUESTION 01

A table has 2 billion rows and 40 columns of 8 bytes each. A query sums one column. Before compression, how much does a column store read compared with a row store?

Choose an answer. You’ll see the explanation once you get it right.

0 of 3 checkpoints answered correctly to complete this lesson.