Athena Query Optimizer
Quick start
- Data scanned is the cost driver. Athena charges $5/TB scanned. The
fastest path to savings is reducing data scanned: partition pruning (filter
by partition column), columnar pruning (Parquet — only read needed columns),
and CTAS materialization (pre-compute aggregations).
- Decision framework (apply in order):
- File format: CSV/JSON → Parquet (10-100x faster, 50-90% less scanned).
- Partitioning: unpartitioned → partition by date/time column. Partitioned
→ add partition projection if > 100K partitions.
- Query rewriting: SELECT * → SELECT specific columns; missing WHERE on
partition column → add filter; COUNT(DISTINCT) → APPROX_COUNT_DISTINCT.
- Workgroup: no data scanned limit → set one. No timeout → set one.
- CTAS: repeated full-table scans → materialize the aggregation.
- Advanced: bucketing for JOIN-heavy workloads, result reuse for repeated
queries.
- Parquet + Snappy is the default recommendation. Columnar storage +
fast decompression = 10-100x faster scans and 50-90% less data read. Every
CSV/JSON table in Athena is an optimization opportunity.
- $5/TB makes small wins matter. A query scanning 500 GB costs $2.50. If
it runs 100 times/day, that is $250/day = $7,500/month. Partition pruning
to scan 10 GB drops it to $150/month.
STRICT output contract
Every optimization response MUST emit this block per target (table, query, or
workgroup). No prose before or after the block.
TARGET: <database/table | query-id | workgroup>
VERDICT: OPTIMIZED | OPPORTUNITY_FOUND | ALREADY_OPTIMAL
REASON: <1-2 sentences naming the recommendation and the supporting data>
RECOMMENDATION:
Current: <file format | partitioning | query pattern | workgroup config>
Proposed: <file format | partitioning | query pattern | workgroup config>
File format: <CSV | JSON | Parquet | ORC>
Compression: <none | GZIP | Snappy | ZSTD>
Partitioning: <none | date column | partition projection>
Query rewrite: <avoid SELECT * | add partition filter | APPROXIMATE | none>
Workgroup: <data scanned limit | query timeout | result reuse>
CTAS: <materialization target | none>
Confidence: <HIGH/MEDIUM/LOW> — <one-line rationale>
ESTIMATED_SAVINGS:
Cost (monthly): $<amount> (<current scan> → <proposed scan>)
Performance: <current runtime> → <proposed runtime>
Data scanned per query: <current> → <proposed>
Annual total: $<amount>
Assumptions: <$5/TB, query frequency, etc.>
MIGRATION_STEPS:
1. <specific action with DDL or CLI command>
2. <verification step>
CONFIRM: Before executing any CTAS or workgroup config change, emit and await
operator approval.
Do NOT omit any field. If a field is not applicable, write N/A with a
one-line reason.
Quick navigation
| Section |
What it covers |
When to read |
| Quick start |
Decision framework order, Parquet rule of thumb |
First read |
| STRICT output contract |
Mandatory output block format |
Before emitting |
| Mindset |
Data-scanned-first philosophy, Parquet dominance |
Understanding |
| Quick reference |
Verdict thresholds |
Classifying findings |
| Pre-flight |
Data gate — query history, DDL, data lake layout |
Before decisions |
| Process |
Ordered optimization steps (1-10) |
Choosing recommendations |
| Output format |
Worked example (OPPORTUNITY_FOUND) |
Formatting |
| Expert heuristic |
Non-obvious Athena behaviours table |
Complex decisions |
| NEVER |
Top 5 anti-patterns |
Before remediation |
references/ |
CLI commands, worked examples, full heuristics |
Deep reference |
Mindset
Athena optimization is a layered decision. Four behaviours separate a senior
analytics engineer from a generalist:
- Data scanned is the only cost lever. Athena charges by bytes read from
S3, not by compute time. Reducing data scanned is the ONLY way to reduce
cost. Everything else (compression, caching, result reuse) reduces bytes
read as a side effect.
- File format is the highest-leverage single change. Switching from CSV
to Parquet reduces data scanned by 50-90% (columnar pruning + compression)
and improves query speed by 10-100x. This is always Step 1.
- Partitioning amplifies file format gains. A Parquet table partitioned
by date lets a query scanning one day of a year-long dataset read 1/365th
of the data. Unpartitioned, it scans the full table even in Parquet.
- Workgroup settings prevent runaway spend. A data-scanned limit caps
the worst-case cost of any single query. Without it, a single
SELECT *
on a petabyte table costs $5,000.
Quick reference — verdict thresholds
| Observation |
Verdict |
Recommendation |
| Table in CSV or JSON format |
OPPORTUNITY_FOUND (file format) |
Step 2 — CTAS to Parquet + Snappy |
| Table unpartitioned, queries filter by date/time |
OPPORTUNITY_FOUND (partitioning) |
Step 3 — add partition column + ALTER TABLE |
| Partitioned table with > 100K partitions, slow metadata ops |
OPPORTUNITY_FOUND (partition projection) |
Step 4 — enable partition projection |
Query uses SELECT * |
OPPORTUNITY_FOUND (query rewrite) |
Step 6 — select only needed columns |
| Query scans full table without partition filter |
OPPORTUNITY_FOUND (query rewrite) |
Step 6 — add WHERE on partition column |
| Repeated full-table aggregation scan |
OPPORTUNITY_FOUND (CTAS) |
Step 7 — materialize the aggregation |
| Workgroup has no data scanned limit |
OPPORTUNITY_FOUND (workgroup) |
Step 8 — set data scanned limit |
| Parquet + partitioned + partition projection + tight queries |
ALREADY_OPTIMAL |
None — continue monitoring |
| Post-remediation: changes applied and verified |
OPTIMIZED |
None — verification passed |
Pre-flight: data gate (run before any optimization decision)
Required data: table DDL (file format, partition columns, compression),
query history (data scanned per query, frequency), and data lake S3 layout.
Key commands: aws glue get-table, aws glue get-partitions,
aws athena batch-get-query-execution, aws s3 ls.
Full CLI sequences in references/athena-optimization-reference.md.
Data-quality short-circuits
| Condition |
Effect on optimization |
| No query history available |
MEDIUM confidence; base estimates on DDL + S3 size |
| Table not in Glue catalog |
NEED_MORE_INFO: register in Glue first |
| Federated query (non-S3 source) |
Different cost model — connector-dependent; not $5/TB |
| Table has 0 bytes (empty) |
Skip — no cost to optimize |
Process — Optimization logic (apply in order)
Step 1: Validate input and data sufficiency
Diagnostic CLI (table DDL and properties, query history, batch execution
details — data scanned, runtime, status): Diagnostic commands.
Step 2: File format optimization (highest-leverage single change)
| Current format |
Target format |
Speedup |
Data scanned reduction |
| CSV (row-based, no compression) |
Parquet + Snappy |
10-100x |
50-90% |
| JSON (row-based) |
Parquet + Snappy |
10-50x |
50-80% |
| Parquet + uncompressed |
Parquet + Snappy |
2-3x |
20-40% |
| Parquet + GZIP |
Parquet + Snappy |
1.5-2x |
5-15% (Snappy faster, similar ratio) |
| ORC + ZSTD |
Keep (already columnar) |
— |
— |
Parquet vs CSV anatomy: CSV is row-based and stores all columns for each
row. A query selecting 3 of 50 columns still reads all 50 columns. Parquet is
columnar: a query selecting 3 of 50 columns reads only those 3 columns from
disk. This is columnar pruning, and it is the #1 reason Parquet is 10-100x
faster.
CTAS migration to Parquet:
-- Create the Parquet table from the CSV table:
CREATE TABLE <db>.<table>_parquet
WITH (
format = 'PARQUET',
parquet_compression = 'SNAPPY',
external_location = 's3://<bucket>/<prefix>/parquet/'
) AS
SELECT * FROM <db>.<table>_csv;
Step 3: Partitioning optimization
Partitioning splits data into S3 subdirectories by column value. Queries with
a WHERE clause on the partition column read only the matching partitions.
| Partitioning state |
Recommendation |
| Unpartitioned, queries filter by date/time |
Add partition by date (e.g., dt='2026-08-01') |
| Partitioned by high-cardinality column (e.g., user_id) |
Switch to bucketing or secondary partition by date |
| Over-partitioned (> 100K partitions) |
Enable partition projection (Step 4) |
| Partitioned by hour (too many small files) |
Consolidate to daily partitions |
Add partitioning via CTAS:
CREATE TABLE <db>.<table>_partitioned
WITH (
format = 'PARQUET',
parquet_compression = 'SNAPPY',
partitioned_by = ARRAY['dt'],
external_location = 's3://<bucket>/<prefix>/partitioned/'
) AS
SELECT col1, col2, ..., date_format(event_time, '%Y-%m-%d') AS dt
FROM <db>.<table>_unpartitioned;
Optimal partition size: 100 MB - 1 GB per partition file. Too many small
files (< 100 MB) cause S3 request overhead. Too few large files (> 1 GB)
reduce parallelism.
Step 4: Partition projection
Partition projection lets Athena calculate partition values mathematically
instead of querying Glue for each partition. Essential for tables with
10K-100K+ partitions.
When to use: Glue get-partitions is slow (> 5s), partition count >
100K, or partitions follow a predictable pattern (date ranges, integer
ranges).
-- Enable partition projection on an existing table:
ALTER TABLE <db>.<table> SET TBLPROPERTIES (
'projection.enabled' = 'true',
'projection.dt.type' = 'date',
'projection.dt.range' = '2024-01-01,2026-12-31',
'projection.dt.format' = 'yyyy-MM-dd',
'projection.dt.interval' = '1',
'projection.dt.interval.unit' = 'DAYS',
'storage.location.template' = 's3://<bucket>/<prefix>/${dt}'
);
Partition projection eliminates the need for MSCK REPAIR TABLE or
ALTER TABLE ADD PARTITION — Athena auto-discovers partitions within the
configured range.
Step 5: Compression optimization
| Compression |
Ratio |
Speed |
Recommendation |
| None (uncompressed) |
1:1 |
Fastest read |
Never use for Athena |
| Snappy |
~3:1 |
Fastest decompression |
Default for Parquet |
| GZIP |
~4:1 |
2-3x slower than Snappy |
Use for cold storage; hurts query speed |
| ZSTD |
~4:1 |
Between Snappy and GZIP |
Good for large datasets; level 3-9 |
| LZ4 |
~2.5:1 |
Faster than Snappy |
Alternative for latency-sensitive |
Rule of thumb: Use Snappy for Parquet (best speed/ratio balance). Avoid
GZIP for Athena queries — the decompression overhead outweighs the better
compression ratio for query workloads. ZSTD is a good upgrade from Snappy for
very large datasets where the better ratio reduces S3 GET costs.
Step 6: Query rewriting
| Pattern |
Problem |
Fix |
Impact |
SELECT * |
Reads all columns, even unused |
SELECT col1, col2 (only needed) |
50-90% scan reduction |
| No WHERE on partition column |
Full table scan |
WHERE dt >= '2026-07-01' |
Proportional to partition pruning |
COUNT(DISTINCT col) |
Exact count is expensive |
APPROX_COUNT_DISTINCT(col) |
10-50x faster, < 3% error |
ORDER BY without LIMIT |
Sorts entire dataset |
Add LIMIT N or use window functions |
Reduces sort overhead |
LIKE '%pattern%' |
Full scan, no optimization |
Use string functions or pre-process |
Variable |
| Repeated subqueries |
Recomputes same data |
Use CTE (WITH) or materialize via CTAS |
Eliminates redundant scans |
JOIN on unbucketed large tables |
Shuffle-heavy |
Bucket both tables on join key (Step 9) |
Eliminates shuffle |
UNION (not UNION ALL) |
Deduplication overhead |
Use UNION ALL if no dupes expected |
Eliminates sort |
-- Before (scans all columns + all partitions):
SELECT * FROM sales WHERE amount > 100;
-- After (only needed columns + partition filter):
SELECT order_id, customer_id, amount
FROM sales
WHERE dt >= '2026-07-01' AND amount > 100;
Step 7: CTAS materialization
If a query (or query pattern) runs frequently and scans a large table to
produce a small result, materialize it with CTAS.
When to use CTAS:
- Daily/weekly aggregation queries scanning raw event data.
- Complex JOINs that produce a denormalized reporting table.
- Queries that scan > 100 GB to produce < 1 GB of results.
-- Materialize a daily aggregation from raw events:
CREATE TABLE <db>.daily_summary
WITH (
format = 'PARQUET',
parquet_compression = 'SNAPPY',
partitioned_by = ARRAY['dt'],
external_location = 's3://<bucket>/curated/daily_summary/'
) AS
SELECT
date_trunc('day', event_time) AS day,
product_id,
COUNT(*) AS event_count,
SUM(amount) AS total_amount
FROM <db>.raw_events
WHERE dt >= '2026-01-01'
GROUP BY 1, 2;
Cost trade-off: The CTAS itself scans data once (costs $X). Subsequent
queries on the materialized table scan far less (costs $X/N per query).
Break-even depends on query frequency. If the daily summary is queried 10+
times/day, CTAS pays for itself within a day.
Step 8: Workgroup settings
| Setting |
Purpose |
Recommendation |
| Data scanned limit (bytes) |
Caps worst-case cost per query |
Set to p99 of healthy queries × 2 |
| Query timeout (minutes) |
Prevents runaway queries |
30 min for interactive; 6 hours for batch |
| Requester pays |
Shifts cost to query requester |
Enable for shared workgroups |
| Result reuse |
Caches identical query results |
Enable for dashboard workloads |
| Enforced workgroup config |
Prevents users from bypassing limits |
Set EnforceWorkGroupConfiguration: true |
# Update workgroup with data scanned limit:
aws athena update-work-group \
--work-group <wg> \
--configuration '{
"ResultConfiguration": {"OutputLocation": "s3://<bucket>/results/"},
"EnforceWorkGroupConfiguration": true,
"PublishCloudWatchMetricsEnabled": true,
"BytesScannedCutoffPerQuery": 10737418240,
"EngineVersion": {"SelectedEngineVersion": "Athena engine version 3"}
}'
Data scanned limit of 10 GB caps any single query at $0.05. Adjust
based on your workload's normal query sizes.
Step 9: Bucketing for JOIN optimization
Bucketing pre-splits data into N files by a hash of the bucket column.
When two tables are bucketed on the same join key with the same bucket
count, Athena can perform a bucket-map JOIN without shuffling.
-- Create bucketed tables for a large JOIN:
CREATE TABLE <db>.orders_bucketed
WITH (
format = 'PARQUET',
parquet_compression = 'SNAPPY',
bucketed_by = ARRAY['customer_id'],
bucket_count = 256,
external_location = 's3://<bucket>/bucketed/orders/'
) AS SELECT * FROM <db>.orders;
CREATE TABLE <db>.customers_bucketed
WITH (
format = 'PARQUET',
parquet_compression = 'SNAPPY',
bucketed_by = ARRAY['customer_id'],
bucket_count = 256,
external_location = 's3://<bucket>/bucketed/customers/'
) AS SELECT * FROM <db>.customers;
When bucketing helps: Large-table-to-large-table JOINs (both > 10 GB).
Not helpful for dimension tables < 1 GB (Athena broadcasts small tables
automatically).
Step 10: Impact estimation and final verdict
File format savings = (csv_bytes_scanned - parquet_bytes_scanned) * $5/TB * query_freq
Partition pruning savings = (full_table_scan - partition_scan) * $5/TB * query_freq
CTAS savings = (raw_scan - materialized_scan) * $5/TB * query_freq - ctas_creation_cost
Verdict: any dimension with a concrete recommendation → OPPORTUNITY_FOUND.
All dimensions pass → ALREADY_OPTIMAL. Changes applied and verified →
OPTIMIZED.
Output format
See STRICT output contract above. Worked example below. Additional examples
(CSV to Parquet migration, ALREADY_OPTIMAL, NEED_MORE_INFO) in
references/athena-optimization-reference.md.
Worked example — CSV to Parquet + partitioning + workgroup limit
TARGET: analytics_db.events_raw
VERDICT: OPPORTUNITY_FOUND
REASON: Table is unpartitioned CSV (1.2 TB). Top 10 queries scan the full
table averaging 320s runtime. Migrating to Parquet + partitioning by date
reduces average scan from 1.2 TB to 18 GB (daily queries). Adding a 10 GB
data-scanned limit prevents runaway scans.
RECOMMENDATION:
Current: CSV, unpartitioned, no compression, no workgroup limit
Proposed: Parquet + Snappy, partitioned by dt, 10 GB workgroup limit
File format: CSV → Parquet
Compression: none → Snappy
Partitioning: none → partitioned_by = ['dt']
Query rewrite: SELECT * → SELECT 8 of 42 columns; add WHERE dt filter
Workgroup: no limit → 10 GB data scanned per query
CTAS: materialize daily_summary aggregation table
Confidence: HIGH — DDL confirms CSV/unpartitioned, query history shows
daily date-filtered queries scanning 1.2 TB each.
ESTIMATED_SAVINGS:
Cost (monthly): $3,280.50 → $49.10
(1.2 TB × $5 × 220 queries/month → 18 GB × $5 × 220 queries/month)
Performance: 320s avg → 8s avg (40x faster)
Data scanned per query: 1.2 TB → 18 GB (98.5% reduction)
Annual total: $38,774.40
Assumptions: $5/TB, 220 daily-filtered queries/month, 1/30th data per
partition after date partitioning + columnar pruning.
MIGRATION_STEPS:
1. Create the Parquet partitioned table via CTAS:
CREATE TABLE analytics_db.events_parquet
WITH (format='PARQUET', parquet_compression='SNAPPY',
partitioned_by=ARRAY['dt'],
external_location='s3://<bucket>/events/parquet/')
AS SELECT event_id, user_id, event_type, amount, device, country,
ip_address, session_id,
date_format(event_time, '%Y-%m-%d') AS dt
FROM analytics_db.events_raw;
2. Verify row count parity:
SELECT COUNT(*) FROM analytics_db.events_parquet;
SELECT COUNT(*) FROM analytics_db.events_raw;
3. Update downstream queries to use the new table + partition filter:
SELECT event_type, COUNT(*) FROM analytics_db.events_parquet
WHERE dt >= '2026-07-01' GROUP BY event_type;
4. Set the workgroup data-scanned limit:
aws athena update-work-group --work-group primary \
--configuration '{"BytesScannedCutoffPerQuery":10737418240,...}'
5. Monitor query performance for 7 days before decommissioning the CSV table.
CONFIRM: Before running the CTAS (scans 1.2 TB, cost ~$6.00), emit and await:
"CONFIRM: About to run CTAS migration on analytics_db.events_raw (1.2 TB,
estimated cost $6.00). Proceed? (yes/no)"
Verdict semantics
| Verdict |
When to emit |
| OPPORTUNITY_FOUND |
At least one dimension has a concrete, savings-bearing recommendation. |
| OPTIMIZED |
Changes applied and verified this session; queries confirm improvement. |
| ALREADY_OPTIMAL |
Parquet + partitioned + tight queries + workgroup limits in place. |
Expert heuristic — consolidated
Full heuristic table (cost lever is bytes scanned, columnar pruning, partition pruning, partition projection, Snappy vs GZIP, APPROX_COUNT_DISTINCT, CTAS break-even, workgroup limits, engine v3 dynamic partition pruning, result reuse, federated pricing, small files): Advanced patterns.
NEVER (top 5)
NEVER recommend changing query patterns without first checking the file
format. A SELECT * on Parquet is still better than SELECT * on CSV, but
SELECT col1, col2 on CSV is still expensive. Fix file format first.
NEVER recommend partitioning without estimating partition size. Too
many small partitions (< 100 MB each) cause S3 503 errors and metadata
bloat. Consolidate to daily or monthly partitions if hourly files are small.
NEVER recommend GZIP compression for Athena Parquet tables. GZIP's
decompression overhead negates the compression advantage for query
workloads. Use Snappy (default) or ZSTD.
NEVER set a workgroup data-scanned limit without first measuring normal
query sizes. Setting it too low blocks legitimate queries. Measure p99
first, then set limit at p99 × 2.
NEVER recommend CTAS materialization without checking query frequency.
A CTAS that costs $5 to create but is queried once/month does not break
even. Materialize only if query frequency justifies the one-time cost.
Pre-flight safety checks (run before any remediation)
- MANDATORY CONFIRMATION GATE before any CTAS (it scans data and costs $).
- Verify row count parity after CTAS migration before decommissioning
the source table.
- Verify partition count after adding partition projection — if the
projection range is wrong, Athena may miss partitions.
- Batch limit: max 5 table migrations per batch, single CONFIRM per batch.
- Capture pre-state: record current DDL and query history before changes.
Recent AWS features (2024-2026)
Recent AWS features (result reuse, federated queries, Spark notebooks, engine v3, Iceberg, parameterized queries): Advanced patterns.
References (load on demand)
- Advanced patterns — consolidated expert-heuristic table (non-obvious Athena behaviours) and recent AWS features (result reuse, federated queries, Spark notebooks, engine v3, Iceberg, parameterized queries)
- Diagnostic commands — Step 1 data-gathering CLI (Glue DDL, query history, batch-get execution statistics)
references/athena-optimization-reference.md — pre-flight CLI sequences, CTAS templates, partition projection configs, workgroup commands, cost formulas, additional worked examples, full NEVER list
references/failure-decision-tree.md — per-step worked examples and the cross-category decision flowchart
Domain
AWS CloudOps / Amazon Athena Analytics Query Performance & Cost Optimization.
AWS documentation
1---2name: athena-query-optimizer3description: Optimizes Amazon Athena query performance and cost via a layered analysis framework covering partitioning (partition projection, dynamic partition pruning), file format selection (Parquet vs CSV vs JSON — Parquet is 10-100x faster via columnar pruning), compression (Snappy vs GZIP vs ZSTD), bucketing for JOIN optimization, workgroup settings (data scanned limits, query timeout), CTAS (CREATE TABLE AS SELECT for materialization), query rewriting (avoid SELECT *, enforce partition filters, use APPROXIMATE functions), and cost modeling ($5/TB scanned). Supports Athena query result reuse, Athena federated queries, and Spark notebook integration. Emits a deterministic verdict (OPTIMIZED | OPPORTUNITY_FOUND | ALREADY_OPTIMAL) with estimated cost and performance impact. Use for Athena cost reduction, slow query tuning, partition projection setup, file format migration, or workgroup spend limits.4license: Apache-2.05compatibility: Agent runtime that reads SKILL.md (Claude Code, Cursor, Windsurf, Codex, Gemini). Offline recommendation classification works from pasted DDL, EXPLAIN output, and query history. Live-account optimization uses aws athena get-query-execution, start-query-execution, batch-get-query-execution, get-work-group, aws ce get-cost-and-usage for Athena spend, aws glue get-table, get-partitions for table metadata, and aws s3 ls for data lake layout inspection (AWS CLI v2, SSO or key-based credentials).6metadata:7 source: https://skillmd.com/skills/jhkchan/athena-query-optimizer8 domain: aws-cloudops9 complexity: high10 requires_llm: 'true'11 phase: '3'12 supports_pipeline: 'true'13 entry_point: 'false'14 family: Analytics15 task_type: optimize16 skill_class: capability17 lifecycle_status: active18 verdict_shape: OPTIMIZED | OPPORTUNITY_FOUND | ALREADY_OPTIMAL19 when_to_use: 'Optimizing Athena query performance and cost: reducing data scanned via partitioning and columnar formats, setting up partition projection, evaluating CTAS for materialization, tuning workgroup settings (data scanned limits, query timeout), rewriting queries (avoid SELECT *, add partition filters, use APPROXIMATE functions), or planning a file format migration from CSV/JSON to Parquet.'20 when_not_to_use: Troubleshooting Athena query failures (use a troubleshoot skill), auditing Athena workgroup security (use the audit-athena-workgroup skill), building a data lake from scratch (use a deploy skill), or Athena Glue crawler troubleshooting (use a Glue troubleshoot skill). This skill focuses on performance and cost optimization.21 activation_triggers: optimize Athena query, Athena query slow, Athena cost reduction, Athena partition projection, Athena file format Parquet, Athena CTAS optimization, Athena data scanned limit, Athena workgroup settings, Athena SELECT * optimization, Athena $5/TB scanned, Athena result reuse, Athena federated query, reduce Athena spend, Athena query timeout, Athena bucketing22 invocation_schema: 'Input: either (a) an Athena query or workload description with table DDL and query patterns, OR (b) live-account context with workgroup, database, table, and query history. Output: a deterministic TARGET / VERDICT / REASON / RECOMMENDATION / ESTIMATED_SAVINGS / MIGRATION_STEPS block where VERDICT is one of {OPTIMIZED, OPPORTUNITY_FOUND, ALREADY_OPTIMAL} and RECOMMENDATION includes file format, partitioning strategy, query rewrite, workgroup config, and materialization (CTAS) guidance.'23 version: 0.1.024 author: Jacky Chan — AWS Community Builder25 keywords: Athena, query optimization, partitioning, partition projection, Parquet, columnar format, Snappy, compression, bucketing, workgroup, data scanned, CTAS, materialization, SELECT *, APPROXIMATE, $5/TB, result reuse, federated queries, Spark notebook26 tags: aws, athena, analytics, optimize, query-performance, cost-optimization, partitioning, parquet27---2829# Athena Query Optimizer3031## Quick start3233- **Data scanned is the cost driver.** Athena charges $5/TB scanned. The34 fastest path to savings is reducing data scanned: partition pruning (filter35 by partition column), columnar pruning (Parquet — only read needed columns),36 and CTAS materialization (pre-compute aggregations).37- **Decision framework (apply in order):**38 - File format: CSV/JSON → Parquet (10-100x faster, 50-90% less scanned).39 - Partitioning: unpartitioned → partition by date/time column. Partitioned40 → add partition projection if > 100K partitions.41 - Query rewriting: SELECT * → SELECT specific columns; missing WHERE on42 partition column → add filter; COUNT(DISTINCT) → APPROX_COUNT_DISTINCT.43 - Workgroup: no data scanned limit → set one. No timeout → set one.44 - CTAS: repeated full-table scans → materialize the aggregation.45 - Advanced: bucketing for JOIN-heavy workloads, result reuse for repeated46 queries.47- **Parquet + Snappy is the default recommendation.** Columnar storage +48 fast decompression = 10-100x faster scans and 50-90% less data read. Every49 CSV/JSON table in Athena is an optimization opportunity.50- **$5/TB makes small wins matter.** A query scanning 500 GB costs $2.50. If51 it runs 100 times/day, that is $250/day = $7,500/month. Partition pruning52 to scan 10 GB drops it to $150/month.5354## STRICT output contract5556Every optimization response MUST emit this block per target (table, query, or57workgroup). No prose before or after the block.5859```text60TARGET: <database/table | query-id | workgroup>61VERDICT: OPTIMIZED | OPPORTUNITY_FOUND | ALREADY_OPTIMAL62REASON: <1-2 sentences naming the recommendation and the supporting data>63RECOMMENDATION:64 Current: <file format | partitioning | query pattern | workgroup config>65 Proposed: <file format | partitioning | query pattern | workgroup config>66 File format: <CSV | JSON | Parquet | ORC>67 Compression: <none | GZIP | Snappy | ZSTD>68 Partitioning: <none | date column | partition projection>69 Query rewrite: <avoid SELECT * | add partition filter | APPROXIMATE | none>70 Workgroup: <data scanned limit | query timeout | result reuse>71 CTAS: <materialization target | none>72 Confidence: <HIGH/MEDIUM/LOW> — <one-line rationale>73ESTIMATED_SAVINGS:74 Cost (monthly): $<amount> (<current scan> → <proposed scan>)75 Performance: <current runtime> → <proposed runtime>76 Data scanned per query: <current> → <proposed>77 Annual total: $<amount>78 Assumptions: <$5/TB, query frequency, etc.>79MIGRATION_STEPS:80 1. <specific action with DDL or CLI command>81 2. <verification step>82CONFIRM: Before executing any CTAS or workgroup config change, emit and await83 operator approval.84```8586Do NOT omit any field. If a field is not applicable, write `N/A` with a87one-line reason.8889## Quick navigation9091| Section | What it covers | When to read |92|---|---|---|93| **Quick start** | Decision framework order, Parquet rule of thumb | First read |94| **STRICT output contract** | Mandatory output block format | Before emitting |95| **Mindset** | Data-scanned-first philosophy, Parquet dominance | Understanding |96| **Quick reference** | Verdict thresholds | Classifying findings |97| **Pre-flight** | Data gate — query history, DDL, data lake layout | Before decisions |98| **Process** | Ordered optimization steps (1-10) | Choosing recommendations |99| **Output format** | Worked example (OPPORTUNITY_FOUND) | Formatting |100| **Expert heuristic** | Non-obvious Athena behaviours table | Complex decisions |101| **NEVER** | Top 5 anti-patterns | Before remediation |102| `references/` | CLI commands, worked examples, full heuristics | Deep reference |103104## Mindset105106Athena optimization is a layered decision. Four behaviours separate a senior107analytics engineer from a generalist:108109- **Data scanned is the only cost lever.** Athena charges by bytes read from110 S3, not by compute time. Reducing data scanned is the ONLY way to reduce111 cost. Everything else (compression, caching, result reuse) reduces bytes112 read as a side effect.113- **File format is the highest-leverage single change.** Switching from CSV114 to Parquet reduces data scanned by 50-90% (columnar pruning + compression)115 and improves query speed by 10-100x. This is always Step 1.116- **Partitioning amplifies file format gains.** A Parquet table partitioned117 by date lets a query scanning one day of a year-long dataset read 1/365th118 of the data. Unpartitioned, it scans the full table even in Parquet.119- **Workgroup settings prevent runaway spend.** A data-scanned limit caps120 the worst-case cost of any single query. Without it, a single `SELECT *`121 on a petabyte table costs $5,000.122123## Quick reference — verdict thresholds124125| Observation | Verdict | Recommendation |126|---|---|---|127| Table in CSV or JSON format | OPPORTUNITY_FOUND (file format) | Step 2 — CTAS to Parquet + Snappy |128| Table unpartitioned, queries filter by date/time | OPPORTUNITY_FOUND (partitioning) | Step 3 — add partition column + ALTER TABLE |129| Partitioned table with > 100K partitions, slow metadata ops | OPPORTUNITY_FOUND (partition projection) | Step 4 — enable partition projection |130| Query uses `SELECT *` | OPPORTUNITY_FOUND (query rewrite) | Step 6 — select only needed columns |131| Query scans full table without partition filter | OPPORTUNITY_FOUND (query rewrite) | Step 6 — add WHERE on partition column |132| Repeated full-table aggregation scan | OPPORTUNITY_FOUND (CTAS) | Step 7 — materialize the aggregation |133| Workgroup has no data scanned limit | OPPORTUNITY_FOUND (workgroup) | Step 8 — set data scanned limit |134| Parquet + partitioned + partition projection + tight queries | ALREADY_OPTIMAL | None — continue monitoring |135| Post-remediation: changes applied and verified | OPTIMIZED | None — verification passed |136137## Pre-flight: data gate (run before any optimization decision)138139**Required data:** table DDL (file format, partition columns, compression),140query history (data scanned per query, frequency), and data lake S3 layout.141Key commands: `aws glue get-table`, `aws glue get-partitions`,142`aws athena batch-get-query-execution`, `aws s3 ls`.143144Full CLI sequences in `references/athena-optimization-reference.md`.145146### Data-quality short-circuits147148| Condition | Effect on optimization |149|---|---|150| No query history available | MEDIUM confidence; base estimates on DDL + S3 size |151| Table not in Glue catalog | NEED_MORE_INFO: register in Glue first |152| Federated query (non-S3 source) | Different cost model — connector-dependent; not $5/TB |153| Table has 0 bytes (empty) | Skip — no cost to optimize |154155## Process — Optimization logic (apply in order)156157### Step 1: Validate input and data sufficiency158159Diagnostic CLI (table DDL and properties, query history, batch execution160details — data scanned, runtime, status): [Diagnostic commands](references/diagnostic-commands.md).161162### Step 2: File format optimization (highest-leverage single change)163164| Current format | Target format | Speedup | Data scanned reduction |165|---|---|---|---|166| CSV (row-based, no compression) | Parquet + Snappy | 10-100x | 50-90% |167| JSON (row-based) | Parquet + Snappy | 10-50x | 50-80% |168| Parquet + uncompressed | Parquet + Snappy | 2-3x | 20-40% |169| Parquet + GZIP | Parquet + Snappy | 1.5-2x | 5-15% (Snappy faster, similar ratio) |170| ORC + ZSTD | Keep (already columnar) | — | — |171172**Parquet vs CSV anatomy:** CSV is row-based and stores all columns for each173row. A query selecting 3 of 50 columns still reads all 50 columns. Parquet is174columnar: a query selecting 3 of 50 columns reads only those 3 columns from175disk. This is columnar pruning, and it is the #1 reason Parquet is 10-100x176faster.177178**CTAS migration to Parquet:**179180```sql181-- Create the Parquet table from the CSV table:182CREATE TABLE <db>.<table>_parquet183WITH (184 format = 'PARQUET',185 parquet_compression = 'SNAPPY',186 external_location = 's3://<bucket>/<prefix>/parquet/'187) AS188SELECT * FROM <db>.<table>_csv;189```190191### Step 3: Partitioning optimization192193Partitioning splits data into S3 subdirectories by column value. Queries with194a WHERE clause on the partition column read only the matching partitions.195196| Partitioning state | Recommendation |197|---|---|198| Unpartitioned, queries filter by date/time | Add partition by date (e.g., `dt='2026-08-01'`) |199| Partitioned by high-cardinality column (e.g., user_id) | Switch to bucketing or secondary partition by date |200| Over-partitioned (> 100K partitions) | Enable partition projection (Step 4) |201| Partitioned by hour (too many small files) | Consolidate to daily partitions |202203**Add partitioning via CTAS:**204205```sql206CREATE TABLE <db>.<table>_partitioned207WITH (208 format = 'PARQUET',209 parquet_compression = 'SNAPPY',210 partitioned_by = ARRAY['dt'],211 external_location = 's3://<bucket>/<prefix>/partitioned/'212) AS213SELECT col1, col2, ..., date_format(event_time, '%Y-%m-%d') AS dt214FROM <db>.<table>_unpartitioned;215```216217**Optimal partition size:** 100 MB - 1 GB per partition file. Too many small218files (< 100 MB) cause S3 request overhead. Too few large files (> 1 GB)219reduce parallelism.220221### Step 4: Partition projection222223Partition projection lets Athena calculate partition values mathematically224instead of querying Glue for each partition. Essential for tables with22510K-100K+ partitions.226227**When to use:** Glue `get-partitions` is slow (> 5s), partition count >228100K, or partitions follow a predictable pattern (date ranges, integer229ranges).230231```sql232-- Enable partition projection on an existing table:233ALTER TABLE <db>.<table> SET TBLPROPERTIES (234 'projection.enabled' = 'true',235 'projection.dt.type' = 'date',236 'projection.dt.range' = '2024-01-01,2026-12-31',237 'projection.dt.format' = 'yyyy-MM-dd',238 'projection.dt.interval' = '1',239 'projection.dt.interval.unit' = 'DAYS',240 'storage.location.template' = 's3://<bucket>/<prefix>/${dt}'241);242```243244Partition projection eliminates the need for `MSCK REPAIR TABLE` or245`ALTER TABLE ADD PARTITION` — Athena auto-discovers partitions within the246configured range.247248### Step 5: Compression optimization249250| Compression | Ratio | Speed | Recommendation |251|---|---|---|---|252| None (uncompressed) | 1:1 | Fastest read | Never use for Athena |253| Snappy | ~3:1 | Fastest decompression | **Default for Parquet** |254| GZIP | ~4:1 | 2-3x slower than Snappy | Use for cold storage; hurts query speed |255| ZSTD | ~4:1 | Between Snappy and GZIP | Good for large datasets; level 3-9 |256| LZ4 | ~2.5:1 | Faster than Snappy | Alternative for latency-sensitive |257258**Rule of thumb:** Use Snappy for Parquet (best speed/ratio balance). Avoid259GZIP for Athena queries — the decompression overhead outweighs the better260compression ratio for query workloads. ZSTD is a good upgrade from Snappy for261very large datasets where the better ratio reduces S3 GET costs.262263### Step 6: Query rewriting264265| Pattern | Problem | Fix | Impact |266|---|---|---|---|267| `SELECT *` | Reads all columns, even unused | `SELECT col1, col2` (only needed) | 50-90% scan reduction |268| No WHERE on partition column | Full table scan | `WHERE dt >= '2026-07-01'` | Proportional to partition pruning |269| `COUNT(DISTINCT col)` | Exact count is expensive | `APPROX_COUNT_DISTINCT(col)` | 10-50x faster, < 3% error |270| `ORDER BY` without `LIMIT` | Sorts entire dataset | Add `LIMIT N` or use window functions | Reduces sort overhead |271| `LIKE '%pattern%'` | Full scan, no optimization | Use string functions or pre-process | Variable |272| Repeated subqueries | Recomputes same data | Use CTE (WITH) or materialize via CTAS | Eliminates redundant scans |273| `JOIN` on unbucketed large tables | Shuffle-heavy | Bucket both tables on join key (Step 9) | Eliminates shuffle |274| `UNION` (not `UNION ALL`) | Deduplication overhead | Use `UNION ALL` if no dupes expected | Eliminates sort |275276```sql277-- Before (scans all columns + all partitions):278SELECT * FROM sales WHERE amount > 100;279280-- After (only needed columns + partition filter):281SELECT order_id, customer_id, amount282FROM sales283WHERE dt >= '2026-07-01' AND amount > 100;284```285286### Step 7: CTAS materialization287288If a query (or query pattern) runs frequently and scans a large table to289produce a small result, materialize it with CTAS.290291**When to use CTAS:**292- Daily/weekly aggregation queries scanning raw event data.293- Complex JOINs that produce a denormalized reporting table.294- Queries that scan > 100 GB to produce < 1 GB of results.295296```sql297-- Materialize a daily aggregation from raw events:298CREATE TABLE <db>.daily_summary299WITH (300 format = 'PARQUET',301 parquet_compression = 'SNAPPY',302 partitioned_by = ARRAY['dt'],303 external_location = 's3://<bucket>/curated/daily_summary/'304) AS305SELECT306 date_trunc('day', event_time) AS day,307 product_id,308 COUNT(*) AS event_count,309 SUM(amount) AS total_amount310FROM <db>.raw_events311WHERE dt >= '2026-01-01'312GROUP BY 1, 2;313```314315**Cost trade-off:** The CTAS itself scans data once (costs $X). Subsequent316queries on the materialized table scan far less (costs $X/N per query).317Break-even depends on query frequency. If the daily summary is queried 10+318times/day, CTAS pays for itself within a day.319320### Step 8: Workgroup settings321322| Setting | Purpose | Recommendation |323|---|---|---|324| Data scanned limit (bytes) | Caps worst-case cost per query | Set to p99 of healthy queries × 2 |325| Query timeout (minutes) | Prevents runaway queries | 30 min for interactive; 6 hours for batch |326| Requester pays | Shifts cost to query requester | Enable for shared workgroups |327| Result reuse | Caches identical query results | Enable for dashboard workloads |328| Enforced workgroup config | Prevents users from bypassing limits | Set `EnforceWorkGroupConfiguration: true` |329330```bash331# Update workgroup with data scanned limit:332aws athena update-work-group \333 --work-group <wg> \334 --configuration '{335 "ResultConfiguration": {"OutputLocation": "s3://<bucket>/results/"},336 "EnforceWorkGroupConfiguration": true,337 "PublishCloudWatchMetricsEnabled": true,338 "BytesScannedCutoffPerQuery": 10737418240,339 "EngineVersion": {"SelectedEngineVersion": "Athena engine version 3"}340 }'341```342343**Data scanned limit of 10 GB** caps any single query at $0.05. Adjust344based on your workload's normal query sizes.345346### Step 9: Bucketing for JOIN optimization347348Bucketing pre-splits data into N files by a hash of the bucket column.349When two tables are bucketed on the same join key with the same bucket350count, Athena can perform a bucket-map JOIN without shuffling.351352```sql353-- Create bucketed tables for a large JOIN:354CREATE TABLE <db>.orders_bucketed355WITH (356 format = 'PARQUET',357 parquet_compression = 'SNAPPY',358 bucketed_by = ARRAY['customer_id'],359 bucket_count = 256,360 external_location = 's3://<bucket>/bucketed/orders/'361) AS SELECT * FROM <db>.orders;362363CREATE TABLE <db>.customers_bucketed364WITH (365 format = 'PARQUET',366 parquet_compression = 'SNAPPY',367 bucketed_by = ARRAY['customer_id'],368 bucket_count = 256,369 external_location = 's3://<bucket>/bucketed/customers/'370) AS SELECT * FROM <db>.customers;371```372373**When bucketing helps:** Large-table-to-large-table JOINs (both > 10 GB).374Not helpful for dimension tables < 1 GB (Athena broadcasts small tables375automatically).376377### Step 10: Impact estimation and final verdict378379```380File format savings = (csv_bytes_scanned - parquet_bytes_scanned) * $5/TB * query_freq381Partition pruning savings = (full_table_scan - partition_scan) * $5/TB * query_freq382CTAS savings = (raw_scan - materialized_scan) * $5/TB * query_freq - ctas_creation_cost383```384385Verdict: any dimension with a concrete recommendation → **OPPORTUNITY_FOUND**.386All dimensions pass → **ALREADY_OPTIMAL**. Changes applied and verified →387**OPTIMIZED**.388389## Output format390391See STRICT output contract above. Worked example below. Additional examples392(CSV to Parquet migration, ALREADY_OPTIMAL, NEED_MORE_INFO) in393`references/athena-optimization-reference.md`.394395### Worked example — CSV to Parquet + partitioning + workgroup limit396397```text398TARGET: analytics_db.events_raw399VERDICT: OPPORTUNITY_FOUND400REASON: Table is unpartitioned CSV (1.2 TB). Top 10 queries scan the full401 table averaging 320s runtime. Migrating to Parquet + partitioning by date402 reduces average scan from 1.2 TB to 18 GB (daily queries). Adding a 10 GB403 data-scanned limit prevents runaway scans.404RECOMMENDATION:405 Current: CSV, unpartitioned, no compression, no workgroup limit406 Proposed: Parquet + Snappy, partitioned by dt, 10 GB workgroup limit407 File format: CSV → Parquet408 Compression: none → Snappy409 Partitioning: none → partitioned_by = ['dt']410 Query rewrite: SELECT * → SELECT 8 of 42 columns; add WHERE dt filter411 Workgroup: no limit → 10 GB data scanned per query412 CTAS: materialize daily_summary aggregation table413 Confidence: HIGH — DDL confirms CSV/unpartitioned, query history shows414 daily date-filtered queries scanning 1.2 TB each.415ESTIMATED_SAVINGS:416 Cost (monthly): $3,280.50 → $49.10417 (1.2 TB × $5 × 220 queries/month → 18 GB × $5 × 220 queries/month)418 Performance: 320s avg → 8s avg (40x faster)419 Data scanned per query: 1.2 TB → 18 GB (98.5% reduction)420 Annual total: $38,774.40421 Assumptions: $5/TB, 220 daily-filtered queries/month, 1/30th data per422 partition after date partitioning + columnar pruning.423MIGRATION_STEPS:424 1. Create the Parquet partitioned table via CTAS:425 CREATE TABLE analytics_db.events_parquet426 WITH (format='PARQUET', parquet_compression='SNAPPY',427 partitioned_by=ARRAY['dt'],428 external_location='s3://<bucket>/events/parquet/')429 AS SELECT event_id, user_id, event_type, amount, device, country,430 ip_address, session_id,431 date_format(event_time, '%Y-%m-%d') AS dt432 FROM analytics_db.events_raw;433 2. Verify row count parity:434 SELECT COUNT(*) FROM analytics_db.events_parquet;435 SELECT COUNT(*) FROM analytics_db.events_raw;436 3. Update downstream queries to use the new table + partition filter:437 SELECT event_type, COUNT(*) FROM analytics_db.events_parquet438 WHERE dt >= '2026-07-01' GROUP BY event_type;439 4. Set the workgroup data-scanned limit:440 aws athena update-work-group --work-group primary \441 --configuration '{"BytesScannedCutoffPerQuery":10737418240,...}'442 5. Monitor query performance for 7 days before decommissioning the CSV table.443CONFIRM: Before running the CTAS (scans 1.2 TB, cost ~$6.00), emit and await:444 "CONFIRM: About to run CTAS migration on analytics_db.events_raw (1.2 TB,445 estimated cost $6.00). Proceed? (yes/no)"446```447448## Verdict semantics449450| Verdict | When to emit |451|---|---|452| OPPORTUNITY_FOUND | At least one dimension has a concrete, savings-bearing recommendation. |453| OPTIMIZED | Changes applied and verified this session; queries confirm improvement. |454| ALREADY_OPTIMAL | Parquet + partitioned + tight queries + workgroup limits in place. |455456## Expert heuristic — consolidated457458Full heuristic table (cost lever is bytes scanned, columnar pruning, partition pruning, partition projection, Snappy vs GZIP, APPROX_COUNT_DISTINCT, CTAS break-even, workgroup limits, engine v3 dynamic partition pruning, result reuse, federated pricing, small files): [Advanced patterns](references/advanced-patterns.md).459460## NEVER (top 5)461462- **NEVER** recommend changing query patterns without first checking the file463 format. A `SELECT *` on Parquet is still better than `SELECT *` on CSV, but464 `SELECT col1, col2` on CSV is still expensive. Fix file format first.465466- **NEVER** recommend partitioning without estimating partition size. Too467 many small partitions (< 100 MB each) cause S3 503 errors and metadata468 bloat. Consolidate to daily or monthly partitions if hourly files are small.469470- **NEVER** recommend GZIP compression for Athena Parquet tables. GZIP's471 decompression overhead negates the compression advantage for query472 workloads. Use Snappy (default) or ZSTD.473474- **NEVER** set a workgroup data-scanned limit without first measuring normal475 query sizes. Setting it too low blocks legitimate queries. Measure p99476 first, then set limit at p99 × 2.477478- **NEVER** recommend CTAS materialization without checking query frequency.479 A CTAS that costs $5 to create but is queried once/month does not break480 even. Materialize only if query frequency justifies the one-time cost.481482## Pre-flight safety checks (run before any remediation)483484- **MANDATORY CONFIRMATION GATE** before any CTAS (it scans data and costs $).485- **Verify row count parity** after CTAS migration before decommissioning486 the source table.487- **Verify partition count** after adding partition projection — if the488 projection range is wrong, Athena may miss partitions.489- **Batch limit:** max 5 table migrations per batch, single CONFIRM per batch.490- **Capture pre-state:** record current DDL and query history before changes.491492## Recent AWS features (2024-2026)493494Recent AWS features (result reuse, federated queries, Spark notebooks, engine v3, Iceberg, parameterized queries): [Advanced patterns](references/advanced-patterns.md).495496## References (load on demand)497498- [Advanced patterns](references/advanced-patterns.md) — consolidated expert-heuristic table (non-obvious Athena behaviours) and recent AWS features (result reuse, federated queries, Spark notebooks, engine v3, Iceberg, parameterized queries)499- [Diagnostic commands](references/diagnostic-commands.md) — Step 1 data-gathering CLI (Glue DDL, query history, batch-get execution statistics)500- `references/athena-optimization-reference.md` — pre-flight CLI sequences, CTAS templates, partition projection configs, workgroup commands, cost formulas, additional worked examples, full NEVER list501- `references/failure-decision-tree.md` — per-step worked examples and the cross-category decision flowchart502503## Domain504505AWS CloudOps / Amazon Athena Analytics Query Performance & Cost Optimization.506507## AWS documentation508509- **Amazon Athena User Guide** — https://docs.aws.amazon.com/athena/latest/ug/what-is.html510- **Athena performance tuning** — https://docs.aws.amazon.com/athena/latest/ug/performance-tuning.html511- **Athena pricing** — https://aws.amazon.com/athena/pricing/512- **Athena CTAS** — https://docs.aws.amazon.com/athena/latest/ug/ctas.html513- **Partition projection** — https://docs.aws.amazon.com/athena/latest/ug/partition-projection.html514- **Athena query result reuse** — https://docs.aws.amazon.com/athena/latest/ug/query-result-reuse.html515- **Athena federated queries** — https://docs.aws.amazon.com/athena/latest/ug/connect-to-a-data-source.html516- **Athena Spark** — https://docs.aws.amazon.com/athena/latest/ug/notebooks-spark.html517- **Apache Iceberg on Athena** — https://docs.aws.amazon.com/athena/latest/ug/querying-iceberg.html518- **AWS Well-Architected Framework — Performance Efficiency** — https://docs.aws.amazon.com/wellarchitected/latest/performance-efficiency-pillar/welcome.html