Columnar Storage

Difficulty: Advanced

Question

What is columnar storage, and why is it much faster than row storage for analytical (OLAP) queries?

Answer

Columnar storage writes all values of one column together on disk, instead of storing each row's values together. Analytical queries usually touch a few columns across millions of rows, so a columnar engine reads only those columns, compresses them far better because similar values sit side by side, and processes them in tight CPU-friendly batches. The cost is that inserting or updating single rows is slow, which is why OLTP databases stay row-oriented.

Row vs column layout

Consider an orders table with columns id, city, amount, status, created_at:

  • Row store (PostgreSQL, MySQL InnoDB): page 1 holds (1, Pune, 450, PAID, ...), (2, Delhi, 1200, PAID, ...) and so on. Fetching one order reads one place.
  • Column store (ClickHouse, BigQuery, Snowflake, Redshift, DuckDB, Parquet files): one file or segment holds all city values, another holds all amount values.

Now run SELECT city, SUM(amount) FROM orders GROUP BY city on a table with 40 columns. A row store has to read every column of every row from disk. A column store reads just two columns, roughly 5% of the data.

Why compression is so much better

Values in one column share a type and often repeat:

  • Dictionary encoding: replace "Bengaluru", "Mumbai" with small integer codes.
  • Run-length encoding: if data is sorted by city, a million consecutive "Pune" values become one pair (Pune, 1,000,000).
  • Delta encoding: timestamps or ids stored as small differences from the previous value.
  • Bit packing: a status column with 4 distinct values needs only 2 bits per row.

Less data on disk means less I/O, and many engines can operate on compressed data directly.

Other speedups

  • Vectorized execution: operators process a batch of thousands of values from one column at a time, which fits CPU caches and SIMD instructions.
  • Min/max metadata (zone maps): each block stores the minimum and maximum value, so a filter like created_at >= '2026-10-01' skips whole blocks.
  • Late materialization: rows are reassembled only for the values that pass the filters.

Trade-offs

AspectRow store (OLTP)Column store (OLAP)
Fetch one full recordFastSlow, must stitch many columns
Single-row insert/updateFastSlow, usually batched
Aggregation over few columnsSlow on large tablesVery fast
Compression ratioModestHigh
ExamplesPostgreSQL, MySQL, OracleClickHouse, BigQuery, Snowflake, Redshift

Column stores handle writes by buffering them and writing large immutable chunks, merging later (ClickHouse's MergeTree parts, Parquet files rewritten by compaction). Updates and deletes are often expensive or asynchronous.

How this shows up in real systems

A company like Swiggy or Flipkart typically keeps orders in a row-oriented OLTP database for the app, streams changes into a columnar warehouse or lake (Parquet on S3 queried by Spark or Trino), and runs dashboards there. Some systems blend both: PostgreSQL has columnar extensions, and SQL Server offers columnstore indexes.

Reading SUM(amount) by city. A column store only touches the columns the query names.
Row storeColumn store
Data read from diskAll 40 columnsOnly city and amount
CompressionMixed types per pageSimilar values together
ExecutionRow at a timeVectorized batches
Block skippingNeeds an indexMin/max per block

Code examples

A columnar table in ClickHouse

CREATE TABLE orders_analytics (
  order_id    UInt64,
  city        LowCardinality(String),   -- dictionary encoded
  amount_paise UInt64,
  status      LowCardinality(String),
  created_at  DateTime
)
ENGINE = MergeTree
ORDER BY (city, created_at);           -- sorting improves compression and skipping

-- Reads only 3 columns, skips blocks outside October
SELECT city, sum(amount_paise) / 100 AS revenue_rupees
FROM orders_analytics
WHERE created_at >= '2026-10-01'
GROUP BY city
ORDER BY revenue_rupees DESC;

The sort key keeps similar values together, which improves compression and lets min/max metadata skip irrelevant blocks.

Key points

  • Column stores keep each column's values together; row stores keep each row together
  • Analytical queries read few columns over many rows, so column stores read far less data
  • Similar adjacent values enable dictionary, run-length, delta and bit-packing compression
  • Vectorized execution and min/max block skipping add further speed
  • Single-row writes and point lookups of full records are slow in column stores

Common follow-up questions

  • What is the difference between OLTP and OLAP workloads?
  • How does Parquet organise data into row groups and column chunks?
  • How do column stores handle updates and deletes?
  • What is HTAP and which databases attempt it?

Interview tips

  • Use a concrete query with a wide table to explain the I/O savings
  • Name at least two compression techniques; it signals you understand why, not just what
  • Mention the trade-off on writes so the answer does not sound one-sided

Concepts covered

Columnar Storage, OLAP, Column Compression, Vectorized Execution, Parquet

More Database Design questions