Skip to the page
Tecyfy

AWS interview question · medium

How do you speed up queries on large S3 datasets in Amazon Athena?

The short answerReduce the data each query reads: convert the data to compressed Parquet, partition it by the common filters with partition projection, keep files large and sorted, and write queries that use those properties.

The full answer

Reduce the data each query reads: convert the data to compressed Parquet, partition it by the common filters with partition projection, keep files large and sorted, and write queries that use those properties.

1. Convert to Parquet once, with CTAS

CREATE TABLE sales_parquet
WITH (
  format = 'PARQUET',
  write_compression = 'ZSTD',
  partitioned_by = ARRAY['sale_date'],
  external_location = 's3://my-lake/curated/sales/'
) AS
SELECT order_id, customer_id, region, amount, sale_date   -- partition column last
FROM sales_csv;

Parquet lets Athena read only the columns a query uses, and you pay per byte scanned.

2. Partition, and manage partitions with projection

ALTER TABLE sales_parquet SET TBLPROPERTIES (
  'projection.enabled' = 'true',
  'projection.sale_date.type' = 'date',
  'projection.sale_date.format' = 'yyyy-MM-dd',
  'projection.sale_date.range' = '2020-01-01,NOW',
  'storage.location.template' = 's3://my-lake/curated/sales/sale_date=${sale_date}/'
);

With partition projection, Athena computes partitions from these rules instead of looking them up, so there is no MSCK REPAIR TABLE to run and planning stays fast with many partitions. Filter on the partition column with equality or a narrow range.

3. Keep files large and sorted

  • Avoid many small files: they slow planning and can hit S3 request limits.
  • Sort data within files by a frequently filtered column so Parquet statistics let Athena skip row groups.
  • For lookups of single values of a high-cardinality key (one customer), bucket by that key in CTAS (bucketed_by, bucket_count).
  • Or use Iceberg tables, where OPTIMIZE ... REWRITE DATA compacts files.

4. Write efficient queries

  • Name the columns you need instead of SELECT *.
  • Put the larger table on the left of a join and the smaller on the right.
  • ORDER BY ... LIMIT for top-N; approx_distinct instead of COUNT(DISTINCT ...) when exact numbers aren't needed; UNION ALL over UNION.

5. Reuse and pre-compute

  • Turn on query result reuse (up to 7 days) for dashboards that repeat the same query.
  • Build summary tables with CTAS or a scheduled Glue job for reports that aggregate the same data over and over.

6. Check the effect EXPLAIN shows which partitions a query will read; the query statistics show data scanned and time per stage. Compare before and after; converting CSV to partitioned Parquet commonly reduces data scanned by an order of magnitude or more.

Last updated 16 October 2026