Sub topic 1
Why Columnar Storage Matters
Prerequisites
This chapter assumes you know the following:
- Basic familiarity with tabular data and schemas (rows, columns, types).
- Working knowledge of Python, including running short scripts.
- The difference between row-oriented and column-oriented storage, at the level of "rows are stored together" versus "columns are stored together".
You do not need to know anything about Parquet's internals yet. Later chapters cover them.
Rows versus columns on disk
A table is a grid, but a disk stores bytes in a line. A file format must decide how to flatten the grid.
- A row-oriented format writes all the values of row 1, then all the values of row 2, and so on. CSV and most transactional databases work this way.
- A column-oriented (columnar) format writes all the values of column 1, then all the values of column 2, and so on. Parquet works this way.
Neither layout is better in general. Each one favors a different access pattern.
Where columnar storage wins
Analytical queries usually touch a few columns across many rows. Consider this query on a table with 50 columns:
SELECT category, SUM(amount) FROM events GROUP BY category;A row-oriented file forces the reader to scan every row in full, even though the query needs two of the 50 columns. A columnar file lets the reader fetch only the bytes for category and amount. This is called column pruning, and it reduces I/O roughly in proportion to the share of columns skipped.
Columnar layout also compresses better. Values in one column share a type and often a similar range or a small set of distinct values. A column such as category might hold only four distinct strings across a million rows. Placing those values next to each other makes repetition easy to exploit. Later chapters show how encodings and compression codecs take advantage of this.
Finally, grouping values by column lets a file store per-column summaries, such as the minimum and maximum. A reader can use those summaries to skip data that cannot match a filter. This is the subject of the later chapters on statistics and predicate pushdown.
Where columnar storage costs more
Columnar storage has trade-offs:
- Reading a whole row means collecting one value from every column, which touches many separate regions of the file.
- Appending a single row is awkward, because each column must be extended. Parquet files are written in bulk and are not edited in place.
- Writers buffer data in memory so they can build each column before writing it.
For workloads that fetch or update single records by key, a row-oriented store is usually the better fit. Parquet targets scans, aggregations, and filtered reads over large datasets.
A quick demonstration
The script below writes the same table as CSV and as Parquet, then reads one column back from Parquet. It needs pyarrow installed.
import os
import pyarrow as pa
import pyarrow.csv as pacsv
import pyarrow.parquet as pq
table = pa.table({
"id": list(range(1_000_000)),
"category": ["a", "b", "c", "d"] * 250_000,
})
pacsv.write_csv(table, "events.csv")
pq.write_table(table, "events.parquet")
print("csv bytes:", os.path.getsize("events.csv"))
print("parquet bytes:", os.path.getsize("events.parquet"))
only_category = pq.read_table("events.parquet", columns=["category"])
print(only_category.num_rows, only_category.column_names)The Parquet file is much smaller than the CSV, because the repetitive category values compress well and the integers are stored in binary rather than text. The exact sizes depend on your library version. The last read decodes only the category column and leaves id untouched.
Common mistakes
- Assuming a smaller file is always faster. Heavier compression saves space but costs CPU time to decompress.
- Selecting every column out of habit. Reading with no
columns=argument discards the main benefit of the format. - Using Parquet as a log you append to one row at a time. Write batches instead.
What comes next
The next chapter opens a Parquet file and names its parts: row groups, column chunks, pages, and the footer.
Exercise
A table has 40 columns of similar width. A query computes the average of one numeric column over all rows. Roughly what share of the column data does a columnar reader need to read, compared with a row-oriented reader, and why?
Show answer
A columnar reader needs about 1/40 (2.5%) of the column data, because it reads only the one column the query uses. A row-oriented reader must scan every row in full, so it reads all of the data, including the 39 unused columns.
Exercise
Name two reasons a columnar layout compresses better than a row layout for the same data.
Show answer
Values in one column share a type, so they are encoded and compressed together without mixed types between them. Values in a column also tend to repeat or fall in a narrow range, which suits encodings and codecs that exploit repetition.