Databases / Columnar storage (Parquet)

Columnar storage (Parquet)

Six orders, stored two ways. Follow one query through a row store and a column store to see why Parquet reads less, compresses better and skips whole blocks.

Intermediate7 steps~6 min
Row — one order: its four values side by side
Column chunk — one column’s values, stored together
Footer — keeps each chunk’s min and max
// THE SHORT VERSION

In 30 seconds

Most databases store a table row by row: all of order 1, then all of order 2. A columnar format such as Parquet stores it column by column: all the ids, then all the countries, then all the amounts.

A query that totals one column then reads just that column. Similar values sitting together also compress well.

Parquet goes further: it cuts the rows into blocks and notes each block’s smallest and largest value, so a filter can skip blocks that cannot match. The price is slower single-row writes and lookups.

Columnar storage (Parquet), step by step

STEPS01 / 07

Real row groups hold many thousands of rows, with statistics per page as well. Writing or fetching one whole row is slower in a column store, which is why transactional databases store rows.

STEP 01 / 07 A table, stored row by row
THE MATH
6 rows × 4 columns = 24 values row 1: 1, FR, pen, 20
  1. Step 1: A table, stored row by row. A small orders table: 6 rows, 4 columns, 24 values. A row store, the layout most transactional databases use, writes each order’s values next to each other on disk, one whole row after another.
  2. Step 2: One question, every row read. To total the amount column, a row store still has to load every row, because the amounts are scattered between ids, countries and items. It reads 24 values to use 6. On a real table with a hundred columns the waste is far larger.
  3. Step 3: Store by column instead. A column store keeps exactly the same values but writes all of one column together, then all of the next. Nothing is lost: the third value in each column still belongs to the third order.
  4. Step 4: Read only what you need. Now the amounts sit side by side. The reader jumps straight to that column and never touches the other three, so it reads 6 values instead of 24. Analytics queries usually use a few columns of a wide table, which is why this pays off.
  5. Step 5: Like values compress well. Values in one column share a type and often repeat, so they compress well. Run-length encoding stores FR FR FR as one value and a count. Dictionary encoding stores each word once and replaces it with a small number. Parquet uses both.
  6. Step 6: Row groups and min/max. A Parquet file cuts the table into row groups; here, 3 rows each. Inside a group every column is stored as its own chunk, and the footer at the end of the file records the smallest and largest value of each chunk.
  7. Step 7: Skip blocks that cannot match. For amount > 50 the reader checks the footer first. Group 1’s largest amount is 35, so no row in it can match and the whole group is skipped unread. Only group 2’s amount chunk is read: 3 values instead of 24.
VALUES SO FAR
table6 rows × 4 colsinput
row read····cost
col read····cost
SUM····result
country····packed
grp 1····min/max
grp 2····min/max
skipped····rule
SUM >50····result

Spotted a mistake on this page?