Column-Oriented Storage and Compression
Why analytics databases store each column on its own, how that makes data small enough to scan fast, and what it costs for writes and single-row reads.
Overview
An analytics query usually touches a few columns of a very wide table, but across millions or billions of rows. A normal row store keeps every row together on disk, so it has to read all the columns to get the few it needs. A column store keeps the values of each column together instead. The query reads only the columns it names, and each column compresses very well because its values look alike.
Why analytics hurts a row store
Two kinds of work ask very different things of storage. Transaction work (OLTP, online transaction processing) reads and writes a few whole rows by key. Analytics work (OLAP, online analytical processing) scans many rows but only a few columns, and aggregates them.
A typical OLTP query fetches or updates one order by id. It touches a few rows and all of each row's columns, so the best layout is row-oriented, as in PostgreSQL and MySQL. A typical OLAP query asks for total sales by region for last year. It touches millions to billions of rows but only 2 to 5 of maybe 100 columns, so the best layout is column-oriented, as in Parquet files, Vertica and DuckDB.
In a row store, reading 3 columns of a 100-column table still pulls all 100 columns off disk, because they sit side by side in the same pages. About 97 percent of the bytes read are thrown away.
Key concepts
- Column-oriented storage
- Row group and column chunk
- Dictionary encoding
- Run-length encoding
- Bitmap
- Min/max statistics
- Projection (C-Store)
- Vectorized execution
Interview questions
- Why is Parquet preferred over CSV for data lake storage?
- Walk me through how a column store compresses a country column with a billion rows.
- How does a column store handle inserts if its files are sorted and compressed?
- When would you not use a column store?
- Why does sort order matter so much in a column store?
Sign up free to read the full topic
More DDIA topics
- ACID Properties
- B-Trees and Write-Ahead Logs
- Beyond MapReduce: Dataflow Engines
- Change Data Capture (CDC)
- Clocks and Time in Distributed Systems
- Consensus Algorithms: Paxos and Raft
- Consistency Guarantees: Eventual, Causal and Linearizable
- Declarative vs Imperative Query Languages
- Encoding Formats: JSON, Protobuf, Avro, Thrift
- End-to-End Correctness and the Exactly-Once Myth
- Event Streams and Message Brokers
- Fan-Out and the Twitter Timeline Problem
Start free on Cariara · Coding problems · System design studio