Sub topic 13
Controlling file and row group sizes
This chapter assumes you can write a partitioned dataset with ds.write_dataset and know that a Parquet file is split into row groups, and each row group into column chunks. Here you will learn which write_dataset options set the size of files and row groups, and how to choose their values.
Why sizes matter
A Parquet file stores statistics (minimum and maximum values) for each row group. A reader uses them to skip row groups that cannot match a filter. A reader also fetches one column chunk at a time, so row group size sets the unit of work for projection and streaming.
- Many tiny files cost more in listing, opening and metadata reads than they save.
- Row groups that are too small compress poorly and make the footer large.
- Row groups that are too large use more memory per reader and give coarser skipping.
A common starting point is files of roughly 100 MB to 1 GB on disk, and row groups of about 100,000 to 1,000,000 rows. Treat these numbers as defaults to measure against your own data.
The write options
write_dataset accepts these size controls:
| Option | Effect |
|---|---|
| `max_rows_per_file` | Starts a new file once this many rows are written. `0` means no limit. |
| `max_rows_per_group` | Upper bound on rows in one row group. It must not exceed `max_rows_per_file` when that is set. |
| `min_rows_per_group` | Buffers rows until this many are available before writing a row group. This avoids many small groups from small input batches. |
| `max_open_files` | Caps how many files stay open at once. If there are more partitions than this, the writer closes the least recently used file. |
import pyarrow as pa
import pyarrow.dataset as ds
table = pa.table({
"year": [2023] * 250_000 + [2024] * 250_000,
"amount": [1.0] * 500_000,
})
ds.write_dataset(
table,
"sales_sized",
format="parquet",
partitioning=["year"],
partitioning_flavor="hive",
max_rows_per_file=200_000,
min_rows_per_group=50_000,
max_rows_per_group=100_000,
existing_data_behavior="delete_matching",
)
dataset = ds.dataset("sales_sized", format="parquet", partitioning="hive")
for fragment in dataset.get_fragments():
meta = fragment.metadata
print(fragment.path, meta.num_rows, meta.num_row_groups)Each year has 250,000 rows. With a 200,000-row file limit, each year directory holds two files: one with 200,000 rows and one with 50,000. The first file has two row groups of 100,000 rows. The second has one row group of 50,000 rows.
Limits to know
The options count rows, not bytes. File size on disk depends on column types and compression, so check a sample file and adjust the row counts.
max_open_files interacts with partitioning. If incoming data is not sorted by partition key and there are more partitions than open files allowed, the writer reopens files and can produce many small ones. Sort or group the data by partition key before writing.
Exercise
A table has 1,000,000 rows in a single partition. You set max_rows_per_file=400_000 and max_rows_per_group=100_000. How many files and row groups per file result?
Show answer
Three files: two with 400,000 rows (4 row groups each) and one with 200,000 rows (2 row groups).