Sizing, Compressing and Sorting Files for Fast Queries
This lesson explains how the size of files and the way they are compressed and sorted decide how efficiently engines read them, since small files and unsplittable compression are among the most common causes of slow lake queries.
Learning outcomes
After completing this lesson, you will be able to:
- Explain the cost of small files and target suitable file sizes.
- Choose between Snappy, GZIP and ZSTD compression and recognise splittability.
- Sort data and spread requests across prefixes.
- Compact small streaming files.
File sizes
File sizes have a large effect on query performance. Every file an engine reads adds fixed overhead for listing, opening it and reading its metadata, so the aim is files of roughly 128 MB to 1 GB. Many small files, a common result of streaming ingestion that writes a file every few seconds, multiply that overhead until it dominates query time. They are compacted into larger files by Glue or EMR jobs, Athena CTAS statements or, for Iceberg tables, the OPTIMIZE command.
Files can also be too large. A very large file in a format that cannot be split, such as a gzip-compressed CSV, must be read by a single task, which limits parallelism; splittable formats such as Parquet, or compression such as Snappy inside Parquet, avoid this.
The following diagram compares one day's files in a data lake partition before and after compaction.

128 MB–1GB
Target size of data lake files
Sizing files
Compression codecs
Compression reduces storage and the amount of data read, at the cost of CPU time to compress and decompress. Snappy is fast with moderate compression and is the common default for Parquet. ZSTD compresses noticeably better at a moderate extra CPU cost, which pays off when less data scanned means a lower bill. GZIP compresses well but uses more CPU, and for plain text files it has an important limitation, described below the table. The following table summarises the trade-offs.
| Codec | Trade-off |
|---|---|
| Snappy | Fast with moderate compression; the common Parquet default |
| ZSTD | Better compression at moderate CPU cost |
| GZIP | Good compression, more CPU; not splittable for text files |
Raw GZIP text files are not splittable, so one large file is processed by one task. Parquet compresses block by block, so it stays splittable whatever the codec.
The following diagram compares how a GZIP text file and a Parquet file are read.

Compression codecs
Sorting and request rates
Sorting data before writing it improves performance further. When files are sorted by a frequently filtered column, each row group's minimum and maximum values cover a narrow range, so engines can skip more row groups for a given filter.
At very high throughput, request rates also matter. Amazon S3 supports at least 3,500 write requests and 5,500 read requests per second per prefix, and scales further by spreading load across prefixes. Workloads that exceed these rates, such as massively parallel jobs reading or writing one folder, spread their data across more prefixes, which partitioning by date or another key often does naturally.
S3 request rates
Compacting streaming output
Firehose writes clickstream data every minute, producing about 1,400 files of 2 MB each per day. Athena queries over a month take minutes, mostly opening files. A nightly compaction rewrites each day's partition into a few large files, with the effect shown in the following table.
| Measure | Before | After |
|---|---|---|
| Files per day | About 1,400 | About 12 |
| Average file size | 2 MB | About 240 MB |
| Monthly query time | Several minutes | Under 30 seconds |
Increasing the Firehose buffer size and interval reduces the number of files at the source; storing the table in Iceberg allows OPTIMIZE to compact it in place. Snappy-compressed Parquet keeps the compacted files splittable.
Why are large gzip-compressed CSV files slow to process?
Show answer
Gzip files cannot be split, so one task must read each file. Parquet or a splittable compression lets many tasks read in parallel.
Exam Tips
Remember for the exam
- When many tiny files slow Athena and Spark, compact them into files of 128 MB to 1 GB.
- Large GZIP text files cannot be split, so prefer Parquet or a splittable compression.
- Snappy is fast; ZSTD and GZIP compress more at higher CPU cost.
- Sort by frequently filtered columns so more blocks are skipped.
- S3 request rates scale per prefix.