9.2 Redshift & Athena Query Performance Tuning & Memory Management
Key Takeaways
- Amazon Redshift table distribution styles (KEY, EVEN, ALL) control inter-node data movement; matching distribution keys on large join tables eliminates network redistribution (DS_DIST_NONE).
- Amazon Athena query performance and cost scale directly with S3 storage optimization: converting raw CSV/JSON to Apache Parquet/ORC with Snappy compression reduces scan volume by up to 90% while enforcing predicate pushdown.
- Amazon Redshift Auto WLM (Workload Management) combined with Concurrency Scaling dynamically routes analytical queries into prioritized queues, preventing long queries from starving transactional ETL workflows.
- Partition Projection in the AWS Glue Data Catalog allows Athena to calculate partition S3 URIs algorithmically, eliminating Glue metastore metadata bottlenecks and rate limits during execution over millions of partitions.
- Inspecting Redshift diagnostic views (SVL_QUERY_SUMMARY, STL_EXPLAIN, STL_WLM_QUERY) reveals is_diskbased = true events, indicating query execution memory (workmem) exhaustion spilling to disk.
Redshift & Athena Query Performance Tuning & Memory Management
High-performance data analytics on AWS relies on optimizing two distinct query execution engines: Amazon Redshift (a managed Massively Parallel Processing data warehouse) and Amazon Athena (a serverless interactive query engine built on Apache Trino/Presto). This section examines the architectural mechanisms and configuration controls required to maximize throughput and minimize execution latencies in both systems.
Amazon Redshift Performance Optimization
Amazon Redshift achieves fast execution across petabyte-scale datasets using a Massively Parallel Processing (MPP) architecture composed of one Leader Node and multiple Compute Nodes divided into slices. Optimizing Redshift requires eliminating network data redistribution, managing memory allocation, and maintaining storage statistics.
1. Data Distribution Styles & Network Redistribution Costs
When executing joins, Redshift compute nodes must co-locate matching table rows across slices. Inter-node network data movement is the primary cause of query latency.
DISTSTYLE KEY: Hash-distributes table rows based on values in a specified column (DISTKEY).- Best Practice: Set
DISTKEYon the join column of two large tables (e.g.,fact_salesanddim_customeroncustomer_id). - Result: Matching rows reside on the exact same compute node slice, yielding
DS_DIST_NONE(zero network movement).
- Best Practice: Set
DISTSTYLE EVEN: Distributes rows round-robin across all slices.- Best Practice: Used for tables involved in no frequent joins, or to resolve data skew.
- Result: Triggers
DS_DIST_INNERorDS_DIST_BOTH, requiring expensive network data broadcasts across nodes during joins.
DISTSTYLE ALL: Copies the entire table to the first slice of every compute node.- Best Practice: Used for small, slowly-changing dimension tables (< 3 million rows).
- Result: Guarantees local join execution (
DS_DIST_NONE) at the cost of higher storage overhead.
DISTSTYLE AUTO: Redshift automatically manages distribution strategy, starting small tables atALLand altering toEVENas table size scales.
2. Sort Keys (COMPOUND vs. INTERLEAVED)
Sort keys order table rows on disk blocks, enabling Redshift block metadata (Zone Maps) to skip un-referenced data blocks during query execution.
COMPOUND SORTKEY(Default): Sorts data hierarchically based on the listed column order (col1,col2).- Optimal Use Case: Range filtering (
WHERE date >= '2026-01-01') or equality filters matching the primary leading columns.
- Optimal Use Case: Range filtering (
INTERLEAVED SORTKEY: Gives equal weight to every column in the sort key.- Optimal Use Case: Ad-hoc multidimensional filtering across varied columns (e.g., searching by
city,zip, orcategoryindependently). - Trade-off: Significantly higher
VACUUMandANALYZEprocessing overhead during bulk load operations.
- Optimal Use Case: Ad-hoc multidimensional filtering across varied columns (e.g., searching by
3. Workload Management (WLM) & Disk Spill Remediation
Redshift WLM allocates cluster memory across dedicated query queues.
- Auto WLM: Uses machine learning to allocate dynamic execution memory (
workmem) based on query complexity and concurrency needs. - Identifying Memory Spills (
is_diskbased = true): If a query's execution memory exceeds allocated queue memory, intermediate hash aggregation or sort steps spill to disk, degrading performance by 10x–100x.- Diagnosis: Query the
SVL_QUERY_SUMMARYdiagnostic view:SELECT query, stm, seg, step, label, is_diskbased, workmem, rows, bytes FROM svl_query_summary WHERE is_diskbased = 't' ORDER BY query DESC; - Remediation: Assign heavy analytical queries to dedicated high-memory WLM queues, increase
wlm_query_slot_countfor manual WLM, or optimize joins to reduce intermediate dataset sizes.
- Diagnosis: Query the
4. Table Maintenance: VACUUM & ANALYZE
VACUUM: Reclaims disk space from deleted rows (VACUUM DELETE ONLY) and re-sorts unsorted table regions (VACUUM SORT ONLY). Redshift performs Automatic Vacuuming in the background, but manualVACUUMis required after massive ETL updates.ANALYZE: Updates HyperLogLog (HLL) table column statistics used by the query optimizer to choose efficient execution plans (e.g., hash joins vs. nested loops).
Amazon Athena Query Optimization & S3 Storage Design
Amazon Athena SQL commonly charges by data scanned, with rates varying by Region; provisioned capacity is also available. Performance tuning in Athena focuses on drastically reducing S3 data scan volume and eliminating metastore lookup latencies.
1. Storage Format & Compression Optimization
Converting raw unstructured or semi-structured data (CSV, JSON) into columnar formats with compression is the single most impactful Athena optimization.
| Format | Storage Layout | Compression | Key Performance Advantage |
|---|---|---|---|
| Apache Parquet | Columnar | Snappy / ZSTD | Column pruning, Dictionary encoding, Min/Max statistics |
| Apache ORC | Columnar | ZSTD / Snappy | Optimized for heavy Hive/Presto workloads, Stripe indexes |
| JSON / CSV | Row-based | None / Gzip | Scans 100% of bytes regardless of SELECT column filters |
- Predicate Pushdown: Columnar storage allows Athena to read only the specific byte ranges and row groups required by SQL
WHEREclauses, skipping irrelevant data blocks entirely.
2. Solving the Small File Problem
Having millions of small files (e.g., < 128 MB) in S3 severely degrades Athena query performance due to S3 HTTP GET request latency overhead and filesystem metadata listing delays.
- Target File Size: 128 MB to 512 MB per file.
- Compaction via CTAS: Use Athena
CREATE TABLE AS SELECT(CTAS) to aggregate small files into optimal Parquet files:CREATE TABLE analytics_db.compacted_orders WITH ( format = 'PARQUET', parquet_compression = 'SNAPPY', external_location = 's3://my-analytics-bucket/compacted_orders/', partitioned_by = ARRAY['year', 'month'] ) AS SELECT * FROM analytics_db.raw_orders;
3. Partition Projection
In traditional Hive partitioning, Athena executes MSCK REPAIR TABLE or queries the AWS Glue Data Catalog to discover partition locations. For datasets with millions of partitions, Glue Metastore API rate limits introduce query start delays.
- Partition Projection Implementation: Configure table properties directly in the Glue Data Catalog so Athena algorithmically calculates S3 partition locations without making Glue API calls:
TBLPROPERTIES ( 'projection.enabled' = 'true', 'projection.year.type' = 'integer', 'projection.year.range' = '2020,2030', 'projection.month.type' = 'integer', 'projection.month.range' = '1,12', 'projection.month.digits' = '2', 'storage.location.template' = 's3://my-analytics-bucket/orders/year=${year}/month=${month}/' )
Redshift vs. Athena Performance Optimization Comparison
| Feature / Control | Amazon Redshift | Amazon Athena |
|---|---|---|
| Primary Architecture | Managed Data Warehouse (MPP Compute Nodes) | Serverless Distributed Query Engine (Trino/Presto) |
| Join Optimization | DISTSTYLE KEY co-location, DISTSTYLE ALL dimensions | Optimal S3 partition keys, hash join memory scaling |
| Block / File Pruning | COMPOUND / INTERLEAVED Sort Keys | Columnar file formats (Parquet/ORC), Partition Projection |
| Concurrency Management | Auto WLM, Concurrency Scaling Clusters | Workgroups (Query limits, Cost controls, Concurrency quotas) |
| Execution Diagnostics | SVL_QUERY_SUMMARY, STL_EXPLAIN, STL_WLM_QUERY | EXPLAIN and EXPLAIN ANALYZE SQL statements |
An Amazon Redshift query joining a 2 TB orders fact table and a 500 GB customers table takes an excessively long time to execute. Checking STL_EXPLAIN shows a high network redistribution cost with step DS_BCAST_INNER. What is the best optimization to eliminate this network bottleneck?
A data team runs daily queries on Amazon Athena against an S3 bucket containing 50 million small JSON files (10 KB each). Queries are taking 20 minutes to complete and scanning hundreds of gigabytes. Which solution provides the greatest performance improvement and cost reduction?
An Amazon Redshift data warehouse contains a small, static 50 MB store_locations dimension table that is joined with a massive 5 TB sales_transactions table across hundreds of daily queries. Which distribution style should be applied to store_locations?