Theory · Page 19 of 34 · Platforms and data in motion

19. Storage: OLTP and OLAP, row and column stores, Parquet

How data is laid out on disk decides which questions are fast. Systems that run the business (OLTP) read and write a few whole rows at a time and store rows together; systems that analyse it (OLAP) scan a few columns of millions of rows and store columns together, compressed. Parquet is the column layout of the lake.

The same rows laid out by row and by column, and what each query reads
Playback speed

The clip lays out six claims twice. Stored by row, each claim's values sit together, so fetching one claim reads one block, while summing the reserves reads every block. Stored by column, each column's values sit together, so the sum reads one column and fetching a claim reads a piece of every column. The cause column is then sorted and compressed: repeated values become runs, and words become small dictionary codes. Finally a Parquet file is opened: row groups, a column chunk per column, pages inside, and the footer's statistics that let a query skip a row group it does not need.

The intuition: a filing cabinet or a ledger

A claims handler at Harbour Mutual opens one claim: its number, policy, dates, cause, status and reserve, all at once. The actuary asks a different question: the total reserve for each cause, over every claim ever made. The first question wants everything about one row; the second wants two columns of every row.

Picture the claims on paper. Filed one folder per claim, the handler's question is one folder, and the actuary's is every folder in the cabinet. Written as a ledger with one page per column, the actuary reads two pages, and the handler has to look in every page. Neither layout is better. Each is faster for one kind of question, and a data platform chooses the layout for the questions it will be asked.

Remember Store rows together for many small reads and writes of whole records; store columns together, sorted and compressed, for scans of a few columns across many rows.

The theory: workloads, layouts, encoding and Parquet

Online transaction processing (OLTP) runs the business: many short transactions, each reading or changing a few rows by key, with strict consistency (the claims system recording a payment). Online analytical processing (OLAP) analyses it: fewer, larger queries that scan, filter and aggregate many rows on a few columns (the actuary's reserve by cause). The two workloads are why warehouses and lakehouses exist apart from the operational databases (chapter 4).

A row store (PostgreSQL, SQL Server's default tables) keeps all of a row's values together in a page. Reading or updating one row touches one page; an index finds it fast. A column store (the warehouse engines, Parquet files, SQL Server's columnstore indexes) keeps each column's values together. A query reads only the columns it names, and similar values sitting next to each other compress well. Stonebraker's C-Store and Abadi's experiments showed that the gain comes less from the layout alone than from what it enables: compression, operating on compressed data, and processing a column in batches.

Columns compress because their values repeat. Dictionary encoding replaces each distinct value with a small integer code and stores the dictionary once. Run-length encoding stores a run of equal values as the value and a count, which works best when the data is sorted. Bit packing stores small integers in as few bits as they need. A general compressor (Snappy, Zstandard) is applied on top.

The causes of the claims as runs, in key order and sorted by cause (PostgreSQL)
SELECT
    (SELECT COUNT(*) FROM claims) AS claims,
    (SELECT COUNT(DISTINCT cause) FROM claims) AS distinct_causes,
    (SELECT COUNT(*) FROM (SELECT cause, LAG(cause) OVER (ORDER BY claim_id) AS previous FROM claims) r
     WHERE previous IS DISTINCT FROM cause) AS runs_in_key_order,
    (SELECT COUNT(*) FROM (SELECT cause, LAG(cause) OVER (ORDER BY cause, claim_id) AS previous FROM claims) r
     WHERE previous IS DISTINCT FROM cause) AS runs_sorted_by_cause

On the seed, the 60 claims have 8 distinct causes. In key order, as rows arrive, they form 53 runs, barely fewer than rows, so run-length encoding gains little. Sorted by cause, they form 8 runs: one per cause. With a dictionary, each cause needs 3 bits instead of its text: about 93 bytes instead of 497 for the column, before any general compression.

Parquet's layout Apache Parquet is the column file format of the lake. A file is split into row groups (a horizontal slice, typically tens to hundreds of megabytes); inside each, a column chunk holds one column's values for those rows; each chunk is divided into pages, the unit of encoding and compression, with a dictionary page first when dictionary encoding is used. The footer holds the schema and, for every column chunk, statistics such as its minimum, maximum and null count. A reader opens the footer first and skips row groups whose statistics cannot match the filter (predicate pushdown) and columns it does not need (projection). Nested data is stored with repetition and definition levels, the encoding Google's Dremel introduced.

Files are rarely queried alone. Splitting a table's files into folders by a column's value (partitioning, by year or country) lets a query skip whole folders; sorting or clustering within files makes the statistics sharper. Both work only for the columns queries actually filter on, and too many small partitions make every query open thousands of tiny files. Table formats (page 20) add the transaction log that turns a folder of Parquet files into a table.

Why it matters

Harbour Mutual's board figures scan years of claims and premiums by a handful of columns: a columnar, compressed layout reads a fraction of the bytes a row store would. The claims system, recording one payment at a time, needs the opposite. Putting the two workloads on the right layouts is the first decision of the platform in chapter 28.

Layout also shows up in cost: on cloud storage you pay for bytes scanned and stored, so encoding, sorting and partitioning are cost controls, not only speed.

Where it goes wrong

  • Analytics on the operational database. Long scans compete with the transactions that run the business.
  • Row-by-row updates on a column store. Each small change rewrites compressed chunks; batch them.
  • Too many small files. Thousands of tiny Parquet files spend the query's time opening files, not reading data.
  • Partitioning on the wrong column. Folders by a column nobody filters on add files without skipping any.
  • Unsorted data. Statistics that span every value let a reader skip nothing.

Check yourself

Choose an answer to each question, then press Check.

1. Which query does a column store serve best?
2. When does run-length encoding compress a column well?
3. What lets a Parquet reader skip a row group without reading it?
Answer every question first.

Sources

The works this page draws on, summarised in its own words rather than quoted. Read them for the full argument.

  • M. Stonebraker et al., "C-Store: A Column-oriented DBMS", Proceedings of VLDB, 2005Column storage for read-mostly workloads, with compression and sorted projections.
  • D. J. Abadi, S. R. Madden and N. Hachem, "Column-Stores vs. Row-Stores: How Different Are They Really?", Proceedings of SIGMOD, 2008Where the speed of column stores comes from: compression, late materialisation and batch processing.
  • Apache Parquet file format specificationRow groups, column chunks, pages, encodings and the footer's statistics.
  • S. Melnik et al., "Dremel: Interactive Analysis of Web-Scale Datasets", Proceedings of VLDB, 2010Repetition and definition levels for storing nested data in columns.
  • M. Kleppmann, Designing Data-Intensive Applications (O'Reilly, 2017), chapter 3OLTP and OLAP, row and column storage, and column compression in plain words.

Practise it

The chapters that put this theory to work on Harbour Mutual and Kestrel:

The tools to try it in:

  • Model studio · Draw conceptual, logical and physical models, generate DDL for PostgreSQL, Spark SQL and T-SQL, and run it on Harbour Mutual's data in your browser.
  • Platform designer · Build an Azure reference architecture from parts, check it against the requirements, and export its Bicep.

Terms taught here: OLTPOLAPColumn storeParquet

An unhandled error has occurred. Reload 🗙

Reconnecting…

Your connection to the Cloud Data Architecture Lab dropped, probably a Wi-Fi blip. Hold on while it reconnects.

The connection could not be restored. The page will reload in a few seconds.