Sub topic 12
Page index for finer-grained skipping
This chapter assumes you know how row groups, column chunks, and pages are laid out, and how min/max statistics let a reader skip whole row groups. It builds on predicate pushdown.
The problem with row group statistics
Row group statistics are coarse. A row group with 100,000 rows has one min and one max per column. If the data inside is not sorted, that range is wide and almost every filter overlaps it. The reader then has to decode every page in the column chunk, even if only a few rows match.
The page index fixes this by storing statistics for each data page, not just for each column chunk.
What the page index contains
The page index is two structures stored near the footer, outside the data pages. Readers can fetch them without scanning the data.
- The column index holds, for each page in a column chunk, the min value, max value, and whether the page is entirely null.
- The offset index holds, for each page, its byte offset in the file, its compressed size, and the row number of its first row.
The column index tells a reader which pages could match a filter. The offset index tells it where those pages are and which rows they cover. This matters because pages in different columns do not line up. A page of id may cover rows 0 to 9,999, while a page of category covers rows 0 to 24,999. The offset index lets the reader map row ranges across columns, so it can read only the matching pages of each column.
When it helps
The page index pays off when values are clustered within a row group. Sorting by the filter column makes each page's min/max range narrow and mostly non-overlapping. With unsorted data, every page spans nearly the full range and nothing can be skipped.
| Data order | Page min/max ranges | Pages skipped |
|---|---|---|
| Sorted by filter column | Narrow, non-overlapping | Most |
| Random order | Wide, overlapping | Almost none |
Writing and reading with PyArrow
Recent PyArrow versions can write the page index when you ask for it. The write_page_index option is available in PyArrow 13 and later. The example sorts the data by id, which is already true here, and sets a small page size so each row group has several pages.
import pyarrow as pa
import pyarrow.parquet as pq
table = pa.table({
"id": list(range(1_000_000)),
"category": ["a", "b", "c", "d"] * 250_000,
})
pq.write_table(
table,
"events_indexed.parquet",
row_group_size=100_000,
data_page_size=64 * 1024,
compression="zstd",
write_page_index=True,
)
result = pq.read_table(
"events_indexed.parquet",
columns=["id"],
filters=[("id", ">=", 950_000)],
)
print(result.num_rows)The filter is the same as in the earlier pushdown example. Row group statistics already skip nine of the ten row groups. The page index goes further inside the last row group and avoids decoding pages that hold only smaller ids.
Gotchas
- The page index is optional. Older files and some writers do not include it, and readers fall back to row group statistics.
- Not every reader uses it. Support depends on the engine and its version, so check before you rely on it.
- It adds a little metadata to the file. For tiny pages, that overhead can be noticeable.
- Smaller pages give finer skipping but add per-page overhead and reduce compression efficiency.
Exercise
A table has a timestamp column that is random within each row group. You filter on a narrow time window. Why would the page index probably not help, and what change to the data would help?
Show answer
With random order, every page's min/max spans nearly the whole range of the row group, so every page overlaps the filter and none can be skipped. Sorting the data by timestamp before writing makes each page's range narrow and non-overlapping, so the column index can rule out most pages.
Exercise
What does the offset index provide that the column index does not, and why is it needed when reading several columns?
Show answer
The offset index gives each page's byte offset, compressed size, and first row number. The column index only gives min/max values. Pages in different columns cover different row ranges, so the reader uses the offset index to find which pages of each column cover the matching rows.