Skip to the page
Tecyfy

AWS interview question · medium

How can you optimize the performance of an Athena query?

The short answerMake Athena read less data and do less work: store the data as compressed Parquet or ORC in reasonably large files, partition it the way most queries filter, select only needed columns, and write joins and aggregations the way Athena's engine prefers.

The full answer

Make Athena read less data and do less work: store the data as compressed Parquet or ORC in reasonably large files, partition it the way most queries filter, select only needed columns, and write joins and aggregations the way Athena's engine prefers.

Data layout (biggest gains)

  1. Columnar formats. Parquet or ORC instead of CSV or JSON, so only the queried columns are read. Keep data sorted by a commonly filtered column (such as a timestamp) so row-group statistics let Athena skip parts of files.
  2. Compression. ZSTD or Snappy inside Parquet. You pay for bytes scanned, so compression cuts cost directly.
  3. Partitioning for common queries. Partition by what most queries filter on, such as date, but not too finely: many tiny partitions mean many small files and slow planning. Use partition projection (or Glue partition indexes) for tables with many partitions instead of MSCK REPAIR TABLE, and use STRING partition keys so filters push down to the catalog.
  4. File sizes. Avoid many small files; they slow listing and can hit S3 request limits. Compact them with Glue, Spark or an Athena CTAS.
  5. Bucketing helps when queries look up single values of a high-cardinality key, such as one customer ID, but not when they ask for many values.
  6. Iceberg tables add compaction (OPTIMIZE) and sorting, and avoid partition-listing overhead.

Query writing 7. Select only needed columns; avoid SELECT *. 8. Joins: put the larger table on the left and the smaller on the right, because the right side is built into memory. 9. Top-N: use ORDER BY ... LIMIT rather than row_number(); use max_by, min_by and arbitrary instead of window functions for "latest value per key". 10. Approximate functions such as approx_distinct when exact counts are not needed. 11. UNION ALL instead of UNION unless duplicates must be removed. 12. UNLOAD for large result sets, which writes in parallel and in compressed formats.

Service features 13. Query result reuse: return a previous result for an identical query, for up to 7 days (60 minutes by default), which suits dashboards. 14. Materialise shared joins and aggregations with CTAS or Glue jobs so dashboards query a small table. 15. Workgroups set data-scanned limits per query and separate teams' costs.

Check your work with EXPLAIN (which partitions will be read) and with the query statistics in the console, which show data scanned and time per stage.

Last updated 16 October 2026