Practical technical guides / Data engineering

Cluster Parquet Rows to Make Min-Max Filtering More Selective

Measured with pyarrow 26: the same 1,000,000 rows read 0.5 MB when sorted by customer and 8.0 MB when shuffled. Clustering decides what statistics skip.

By Sandeep Belgavi · Published 2026-12-01

Measured with pyarrow 26: the same 1,000,000 rows read 0.5 MB when sorted by customer and 8.0 MB when shuffled. Clustering decides what statistics skip.

Statistics only help when they disagree

Parquet files are split into row groups, and column chunks can carry statistics in the file footer, including minimum and maximum values when the writer supplies them. A query engine that sees WHERE customer_id BETWEEN 50000 AND 50999 can read the footer first and skip every row group whose range for customer_id does not overlap the filter. This is usually described as predicate pushdown, and it is often presented as something Parquet gives you for free.

It is not free. Skipping depends entirely on the values inside each row group. If every row group contains customers from 0 to 199,999, every row group's range overlaps an in-domain equality filter, the statistics rule nothing out, and the engine reads the whole column anyway. Write-time layout strongly affects how much data a selective filter can skip. The predicate, available statistics and reader support also matter; even unsorted data can be excluded when its ranges do not overlap the predicate.

The running example is the order-line archive of Belgavi's Book Store: one million lines a month, written by a nightly job in arrival order and queried mostly by customer. Hansini wants to know why a single-customer lookup reads the same amount of data as a monthly report. The companion example parquet_rowgroup_pruning writes the same synthetic rows twice and measures what a filtered read actually pulls from the file.

Measured: sorted versus shuffled

The example generates 1,000,000 rows with a customer_id between 0 and 199,999, an amount and a short note. It writes them with pyarrow 26.0.1, zstd compression and 50,000 rows per row group, so each file has 20 row groups. One file is sorted by customer_id. The other keeps random order, which is what arrival order looks like for a customer key. It then reads each file through a file object that counts every byte requested, with pyarrow.parquet.read_table, a filter on customer_id and only the customer_id and amount columns.

QueryFile orderRow groups whose statistics overlapRows returnedBytes readFile size
customers 50,000 to 50,999sorted2 of 205,093505,6349,036,993
customers 50,000 to 50,999shuffled20 of 205,0938,002,49310,859,471
one customersorted1 of 206285,6549,036,993
one customershuffled20 of 2068,002,49310,859,471

The overlap column is a calculation from the footer statistics. The bytes column is measured: it is what the reader actually requested. Both queries return the same rows from both files. The sorted file answers the range query by reading about 0.5 MB and the single-customer query by reading about 0.29 MB. The shuffled file reads about 8.0 MB for both, which is nearly all of the two requested columns, because no row group can be excluded. The sorted file is also about 17 percent smaller, since neighbouring values compress better.

Sixteen times more data for the range query and twenty-eight times more for the single customer, with identical rows and identical code, purely from write order.

How the skip works

The Parquet format stores statistics in the column chunk metadata of each row group, and newer writers also store a column index with per-page minimum and maximum values. Readers compare the filter with these ranges before reading data pages. For a range filter on a column whose values are spread uniformly across the file, each row group's range is roughly the whole domain, so the comparison passes for every group. Sorting, or clustering, makes each row group cover a narrow slice of the domain, so most groups fail the comparison and are never read.

The same logic applies to partitioned datasets one level up. Partitioning by month prunes whole files for date filters. Within each file, row-group statistics prune for whichever column the rows are clustered on. A file can only be clustered on one leading column at a time, so choose the column that the most selective, most frequent filters use.

What the numbers do not show

Bytes read is the right first measure, but not the only one. Every read still fetches the footer, so very small files pay a fixed cost that sorting cannot remove. Object stores charge per request as well as per byte, and an engine that skips row groups may issue more, smaller range requests. Query time also depends on caching: a second run of the shuffled query can look fast because the operating system or the engine kept the data in memory. Measure cold and warm runs separately, and count requests as well as bytes when the files live in an object store.

Writing files that can be skipped

For the archive, the nightly job sorts each month's lines by customer_id before writing.

import pyarrow as pa, pyarrow.parquet as pq

def write_month(table: pa.Table, path: str) -> None:
    ordered = table.sort_by([("customer_id", "ascending"), ("order_date", "ascending")])
    pq.write_table(ordered, path, row_group_size=50_000, compression="zstd")

Three settings deserve attention.

Row group size. Smaller row groups give finer skipping but more footer metadata and more seeks. Many engines default to large row groups, measured in megabytes rather than rows. Choose a size, measure the bytes read for your common filters, and adjust.

Secondary columns. With rows sorted by customer_id, a filter on order_date alone skips little within a month's file, because each row group spans the whole month. Partition by date and cluster by customer, or keep a second copy sorted differently if both filters are frequent.

Statistics that are written at all. Writers can disable statistics for some columns, and long string values may be truncated in statistics. Check the footer with pq.ParquetFile(path).metadata.row_group(0).column(i).statistics before assuming a column can prune.

Migrating an existing archive

  1. Measure first. For the five most common filters, record bytes read per query from the engine's own metrics or a counting reader like the companion example.
  2. Pick the clustering column from those filters.
  3. Rewrite one partition sorted, keep the old one, and compare bytes read and query times on the same queries.
  4. Rewrite the rest in the background, partition by partition, and switch readers per partition.
  5. Add a check to the writer that fails if output is not sorted on the clustering column, so a later change to the job does not quietly undo the layout.

Failure modes

Appends that break the order. A job that appends late-arriving rows as new small files keeps each file sorted but spreads one customer across many files. File-level statistics still help, but many small files cost seeks. Compact periodically.

Sorting by a transformed key. Sorting by a string form of a numeric id orders 10000 before 9. Statistics then still prune, but on string ranges, and filters that compare numbers may not use them. Sort and filter on the same type.

Statistics without the engine using them. Some readers ignore statistics for certain types or expressions, for example a filter on lower(note). Measure, do not assume.

Ordering that drifts. A writer refactor that moves to parallel output can interleave sorted chunks. Validate ordering per row group, not just per file.

When clustering does not pay

If almost every query scans whole months for reports, there is nothing to skip and sorting only costs write time. If the filter column has very few distinct values, partitioning by it is simpler than sorting. Clustering pays when queries are selective on one column and frequent enough that read cost dominates write cost.

How to verify it

Measure bytes read, not query time alone, because caches hide layout problems. Count row groups skipped for your common filters with the engine's metrics or the footer, and compare with the total. Re-measure after any change to the writer. The companion example reproduces the table above; change the row group size or the sort key and watch the bytes read move.

Primary references and authorship

Sources checked 2026-12-01. Original explanation and synthetic examples by Sandeep Belgavi. Companion code: Sandeep-BA/agent-decision-lab/examples/python/parquet_rowgroup_pruning. Views are my own and do not represent any employer, past or present.