Lesson 3 of 7 Amazon S3 Data Lakes

Partitioning Data in Amazon S3

This lesson explains how partitioning stores data in folders by the value of one or more columns, so queries filtering on those columns read only the folders they need, and how poorly chosen partitions make queries worse.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe Hive-style partitioning.
  • Choose partition keys from how data is queried.
  • Manage many partitions with partition projection and indexes.
  • Register new partitions in the Data Catalog.

How partitioning works

Partitioned data uses Hive-style folder names, for example s3://lake/sales/year=2026/month=01/day=05/. Queries filtering on partition columns read only matching folders (partition pruning). The Glue Data Catalog records each partition's location, or Athena calculates them with partition projection.

The following diagram shows a partitioned table and the folder a query reads.

Partition pruning on Hive-style folders. The sales table is stored under year=, month= and day= folders. A query with WHERE year = 2026 AND month = 01 AND day = 05 reads only the matching folder; the engine finds its location in the Glue Data Catalog or calculates it with partition projection.
Only some folders are drawn.
Scenario questions

Partition pruning

Exam skills 2.4.5 · 2.2.4

Work out what a partitioned query reads.

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

Choosing partition keys

Partition keys should reflect how the data is queried, because partitioning helps only when queries filter on the partition columns. The usual choice is the columns that appear most often in WHERE clauses, which is almost always a date, sometimes combined with a region or tenant.

Granularity matters as much as the choice of column. Each partition should hold at least a few hundred megabytes, so daily partitions are often enough, and hourly partitions suit only very high volumes. High-cardinality keys, such as user ID, should be avoided: they create millions of tiny partitions, each with a few small files, which makes listing files and planning queries slower than scanning unpartitioned data would be.

The following diagram compares a date partition key with a high-cardinality key.

Partitioning by date compared with partitioning by user ID. Daily partitions match the queries' filters and hold hundreds of megabytes each. User ID partitions create millions of tiny partitions, each with a few small files, so listing and planning take longer than scanning unpartitioned data would.
The file sizes are illustrative.
Sort into groups

A good partition key?

Exam skills 2.4.5

Decide whether each column is a good partition key.

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

Managing many partitions

Partitioning makes queries faster, but very many partitions slow down query planning, because the engine must first list which partitions exist. Three features address this. Athena partition projection calculates partition locations from rules in the table properties, such as a date range and a path template, instead of reading them from the catalogue. It is fastest for predictable partition schemes and needs no crawler or MSCK REPAIR to add new partitions. Glue partition indexes let the catalogue find matching partitions quickly without returning them all.

Iceberg hidden partitioning partitions by a transform of a column, such as day(ts), and tracks files in its own metadata. Queries filter on the column itself, without separate partition columns, and partitioning can change over time without rewriting old data. The following table summarises the features.

FeatureBenefit
Athena partition projectionCalculates partitions from configuration instead of reading the catalogue
Glue partition indexesSpeed up partition filtering in the catalogue
Iceberg hidden partitioningPartitions by a transform such as day(ts) without partition columns in queries, and allows partition evolution

Adding partitions

Engines that rely on the Data Catalog, such as Athena and Redshift Spectrum, see a new partition only after it has been registered there. There are several ways to register partitions. Glue crawlers discover new folders and add them, and in S3 event mode they crawl only the folders that changed, which is much faster than a full crawl. ETL jobs can update the catalogue as they write new data.

In Athena, MSCK REPAIR TABLE scans the table's location and adds any missing partitions, though it becomes slow on large tables, while ALTER TABLE ADD PARTITION adds specific partitions quickly. Partition projection avoids registration altogether: Athena calculates partition locations from a pattern in the table properties, such as a date range, so new partitions are queryable as soon as their data lands.

Match the pairs

Registering partitions

Exam skills 2.2.4

Match each situation with how new partitions become queryable.

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

The cost of a query before and after

A table of web logs holds three years of data (9 TB of gzipped JSON). A daily query for yesterday's errors scans everything. The following table shows the data scanned after each change.

ChangeData scanned per query
Original layout9 TB
Partition by dtAbout 8 GB (one day)
Convert to Parquet with SnappyAbout 1.5 GB
Select only 4 of 40 columnsAbout 150 MB

Partitioning by date makes the largest difference, because the query filters on one day. Partitioning by user ID instead would have created millions of tiny partitions and made every query slower.

Check yourself

Why can partitioning on a high-cardinality key hurt performance?

Show answer

It creates huge numbers of small partitions and files, so planning and listing them costs more than the pruning saves.

Exam Tips

Remember for the exam

  • Partition on columns used in WHERE clauses, usually the date.
  • Over-partitioning on high-cardinality keys hurts performance.
  • For many predictable partitions in Athena, use partition projection.
  • To speed up partition filtering in the catalogue, use partition indexes.
  • Iceberg hidden partitioning removes partition columns from queries.