Partitioning Logs for Speed and Cost
Organize data so investigations run faster and cost less.
Partitioning Logs for Speed and Cost is a free AWS Security Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the AWS Security Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The Cost of Scanning
Because Athena charges by data scanned, querying a year of logs to find one day's events wastes money and time. Partitioning is the technique that lets Athena read only the relevant slice of data, making queries dramatically faster and cheaper. It is the single most important Athena optimization.
What a Partition Is
A partition divides a table's data by the values of one or more columns, usually date components, mapped to S3 prefixes. CloudTrail logs are already laid out by region/year/month/day, so partitioning by these matches the physical structure and lets Athena skip folders that fall outside your query.
Partition Pruning
When a query filters on a partition column, Athena performs partition pruning: it reads only the matching prefixes and ignores the rest. A query for one day with proper partitions scans a tiny fraction of the bucket. Without pruning, the same query would scan everything, even data it cannot possibly need.
Adding Partitions Manually
One way to register partitions is ALTER TABLE ADD PARTITION, mapping a partition value to its S3 location. This works but is tedious for daily log data, since you would add a partition every day. It is fine for one-off historical loads but does not scale for ongoing logs.
Partition Projection
Partition projection is the modern, recommended approach: you tell Athena the partition pattern, such as dates in a range, and it calculates the prefixes at query time without storing each partition in the catalog. For continuously growing CloudTrail and Flow Log data this avoids constant maintenance and keeps pruning automatic.
TBLPROPERTIES (
'projection.enabled'='true',
'projection.dt.type'='date',
'projection.dt.range'='2023/01/01,NOW',
'projection.dt.format'='yyyy/MM/dd'
)Glue Crawlers for Partitions
A Glue crawler can also discover new partitions on a schedule and add them to the catalog. This automates partition management for evolving data, though for predictable date-based layouts partition projection is usually simpler and incurs no crawler cost.
Columnar Formats Compound Savings
Partitioning limits which files are read; columnar formats like Parquet limit which columns are read within those files. Combining both, converting logs to Parquet and partitioning by date, can cut data scanned by orders of magnitude. Many teams run a conversion job to store query-optimized copies of high-volume logs.
Compression
Compressing log data with formats like GZIP or Snappy further reduces bytes scanned and storage cost. Athena reads compressed files transparently. Combined with partitioning and columnar storage, compression rounds out the trio of techniques that make large-scale log analysis affordable.
Designing Partitions for Security
For security logs, partition primarily by date since investigations are time-bounded, and consider partitioning by account or region for organization trails so you can target one account quickly. The right partition keys mirror how your investigations actually filter, maximizing pruning for real queries.
Watching Scan Metrics
Athena reports the data scanned per query. Watching this metric tells you whether partitioning is working: a well-partitioned, time-bounded query should scan megabytes, not terabytes. If a query scans far more than expected, your filter is probably not aligning with the partition columns.
Putting Optimization Together
The optimization stack is partition to skip irrelevant data, columnar format to read only needed columns, and compression to shrink bytes. Applied to CloudTrail and Flow Logs, these turn what would be slow, costly scans into fast, cheap investigations, exactly what large-scale security analysis demands.
Quick Check
Test your partitioning knowledge.
Recap
Partitioning divides table data by columns (usually date) so Athena does partition pruning and scans only relevant prefixes, cutting cost and time. Prefer partition projection over manual ADD PARTITION or crawlers for growing logs. Combine partitions with columnar Parquet and compression for huge savings, partition security logs by date and account, and watch the data-scanned metric.
Frequently asked questions
Is the “Partitioning Logs for Speed and Cost” lesson free?
Yes — the full text of “Partitioning Logs for Speed and Cost” is free to read here on the web, and the AWS Security Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the AWS Security Academy course, upgrade to CoddyKit PRO.
What will I learn in “Partitioning Logs for Speed and Cost”?
Organize data so investigations run faster and cost less. You practise AWS Security Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start AWS Security Academy?
No prior experience is required. AWS Security Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Partitioning Logs for Speed and Cost” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this AWS Security Academy lesson?
Yes. Every AWS Security Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Querying S3 Logs with Athena
- Building Tables Over CloudTrail Data
- Investigating Incidents with SQL Queries
- Partitioning Logs for Speed and Cost