Lesson 4 of 7 Amazon S3 Data Lakes

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.

One day's partition before and after compaction. Firehose writes about 1,400 files of 2 MB a day, so monthly Athena queries spend most of their time opening files. A nightly compaction rewrites each day into about 12 files of 240 MB, and monthly queries finish in under 30 seconds.
The scenario is illustrative.

128 MB–1GB

Target size of data lake files

Scenario questions

Sizing files

Exam skills 2.4.5 · 1.4.1

Choose the fix for each file size problem.

Log in to try this activity Interactive activities are free with an account.

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.

CodecTrade-off
SnappyFast with moderate compression; the common Parquet default
ZSTDBetter compression at moderate CPU cost
GZIPGood 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.

Why a large GZIP text file limits parallelism. A gzip-compressed CSV cannot be split, so one task reads the whole file while the other executors wait. Parquet compresses block by block, so the same data can be split and read by many tasks at once, whatever the codec.
Task counts are illustrative.
Match the pairs

Compression codecs

Exam skills 2.4.5

Match each codec with its trade-off.

Log in to try this activity Interactive activities are free with an account.

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.

Fill in the value

S3 request rates

Exam skills 2.1.2

Recall S3's per-prefix request rates.

Log in to try this activity Interactive activities are free with an account.
Worked example

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.

MeasureBeforeAfter
Files per dayAbout 1,400About 12
Average file size2 MBAbout 240 MB
Monthly query timeSeveral minutesUnder 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.

Check yourself

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.