Amazon Athena

Difficulty: Intermediate

Question

What is Amazon Athena, how is it billed, and how do you make Athena queries fast and cheap?

Answer

Amazon Athena is a serverless, interactive query service that runs standard SQL directly on data stored in Amazon S3, with no database to load and no servers to manage. It uses the Glue Data Catalog for table definitions and a distributed engine based on Trino, and with the default pricing model you pay per terabyte of data your queries scan. That makes the main optimisation simple: scan less data, using columnar formats, compression, and partitioning.

How Athena runs a query

  1. You define a table: a schema plus an S3 location and a file format (CSV, JSON, Parquet, ORC, Avro, or Apache Iceberg tables). The definition lives in the Glue Data Catalog.
  2. You submit SQL from the console, JDBC/ODBC, or the API (StartQueryExecution).
  3. Athena's engine splits the work across many workers that read the S3 files in parallel.
  4. Results are written to an S3 output location you configure and returned to the client.

There is no load step. Drop a new file under the table's prefix and it is queryable immediately (once its partition is known).

Billing model

  • Per-query pricing: charged by data scanned, about $5 per TB in most Regions, rounded up to 10 MB per query. DDL statements and failed queries are not charged.
  • Provisioned capacity: reserve DPUs for a fixed hourly price, useful for heavy, predictable workloads.

Because you pay for bytes read, a SELECT * over a year of uncompressed JSON logs can be expensive, while the same question against partitioned Parquet might scan a few hundred MB.

Making queries fast and cheap

TechniqueWhy it helps
Columnar formats (Parquet, ORC)Reads only the columns in the query and stores min/max stats for skipping
Compression (Snappy, ZSTD)Fewer bytes scanned and transferred
Partitioning (dt=2026-10-12/)WHERE dt = ... skips entire folders
Partition projectionComputes partitions from a pattern, avoiding slow catalog lookups for thousands of partitions
Right-sized files (roughly 128 MB or more)Thousands of tiny files add per-file overhead
Select specific columnsSELECT * defeats columnar storage
CTAS / INSERT INTOConvert raw JSON to partitioned Parquet using Athena itself

Where Athena fits

  • Good for: ad-hoc analysis of logs (ALB, CloudTrail, VPC Flow Logs), querying a data lake, occasional reports, and investigations.
  • Less suited for: dashboards with many concurrent users and sub-second expectations (Redshift or a cache is better), and high-frequency row-level updates (although Iceberg tables support UPDATE, DELETE, and MERGE).
  • Federated queries let Athena query other sources (DynamoDB, RDS, CloudWatch Logs) through Lambda-based connectors.

Gotchas

  • Use workgroups to separate teams, enforce result locations, and set per-query data scan limits so one bad query cannot scan terabytes.
  • If a table's partitions are not registered (and partition projection is off), new data silently returns no rows; run MSCK REPAIR TABLE or ALTER TABLE ADD PARTITION.
  • CSV with commas inside fields needs the right SerDe (OpenCSVSerDe) or columns shift.
How an Athena query runs. Athena reads table metadata from the catalog, scans S3 in parallel, and writes results back to S3.
  1. SQL query submitted: Console, JDBC, or StartQueryExecution API.
  2. Glue Data Catalog: Provides schema, format, and partition locations.
  3. Distributed Trino-based engine: Workers prune partitions and scan in parallel.
  4. S3 data files: Only needed columns and partitions are read.
  5. Results in S3: Billed by bytes scanned.

Code examples

Partitioned table with partition projection, then a cheap query

CREATE EXTERNAL TABLE alb_logs_parquet (
  client_ip      string,
  request_url    string,
  elb_status     int,
  target_ms      double
)
PARTITIONED BY (dt string)
STORED AS PARQUET
LOCATION 's3://acme-logs/alb-parquet/'
TBLPROPERTIES (
  'projection.enabled'      = 'true',
  'projection.dt.type'      = 'date',
  'projection.dt.format'    = 'yyyy-MM-dd',
  'projection.dt.range'     = '2025-01-01,NOW',
  'storage.location.template' = 's3://acme-logs/alb-parquet/dt=${dt}/'
);

-- Scans only one day's partition and two columns
SELECT elb_status, count(*) AS hits
FROM alb_logs_parquet
WHERE dt = '2026-10-11'
GROUP BY elb_status
ORDER BY hits DESC;

Partition projection computes partition locations from the dt pattern, so no crawler or MSCK REPAIR is needed. The WHERE on dt plus Parquet means only a tiny fraction of the data is billed.

Key points

  • Athena = serverless SQL over S3 using the Glue Data Catalog and a Trino-based engine
  • Default billing is per TB scanned (10 MB minimum per query); provisioned capacity is an alternative
  • Parquet/ORC, compression, partitioning, and selecting only needed columns cut cost
  • Partition projection avoids managing thousands of catalog partitions
  • Workgroups enforce limits and separate teams

Common follow-up questions

  • How would you reduce the cost of a query that scans 2 TB per run?
  • What is the difference between Athena and Redshift?
  • What is partition projection and when would you use it?
  • How would you query CloudTrail or ALB logs with Athena?

Interview tips

  • Tie every optimisation back to 'you pay for bytes scanned'; it shows you understand the cost model
  • Give a concrete use case such as querying ALB or CloudTrail logs during an incident
  • Compare with Redshift briefly to show you know when Athena is not the right tool

Concepts covered

Amazon Athena, Serverless SQL, Pay per Data Scanned, Partitioning, Parquet

More AWS & Cloud Fundamentals questions