Skip to main content
86 lines of code → $25,000 a year saved on Athena queries that read 38 KB and paid for 10 MB

86 lines of code → $25,000 a year saved on Athena queries that read 38 KB and paid for 10 MB

· 8 min read
Samuel Cozannet
Co-founder & CPTO, Cloud ex Machina

86 lines of code → $25,000 a year saved.

That's what one team got for batching a set of tiny Athena queries. In a sample, each one read about 38 KB, and Athena's 10 MB minimum charged it for roughly 260 times that. The production account sent 810,000 of them on a median weekday.

The queries also opened a lot of small files in S3. In one month the account paid $1,848 for S3 GET requests, against $15 for the storage. On the first weekday after the change, Athena and S3 GET cost together came to $39, down from $128 the day before.

This is the fourth post of Feedback loops from the FinOps trenches, after a Lambda function whose 100 workers had the throughput of 10.

Athena and S3 GET cost per weekday on one production account, five weekdays before and five after the small queries were batched. Before the change the five days cost 135, 116, 63, 158 and 128 dollars. After it they cost 39, 36, 26, 27 and 27 dollars

How the Athena 10 MB minimum works​

Athena bills SQL queries by the bytes they scan, rounded up to the next megabyte, with a 10 MB minimum per query. Analytics over large tables never notices. A metadata job does, because it asks many small questions, such as "which items of this kind exist here?", and pays the full minimum for the handful of rows that answers each one.

If each of those 810,000 queries reads 38 KB, together they read about 31 GB and get billed for 8.1 TB. At $5 per terabyte, that's about $40 a day for data nobody read.

Why small files make S3 GET requests expensive​

S3 bills every GET request, whatever the size of the object. A query that reads ten small files pays for at least ten requests, and the next query over the same files pays for them again, which added up quickly here because the tables held one file per account and table, each a few kilobytes.

Over the month, that came to 4.62 billion GET requests, at about 120 times the cost of the storage. Those requests show up on the bucket's S3 line of the bill, a long way from the Athena line of the queries that caused them. List and write requests can grow the same way, as in the streaming pipeline that turned a data lake bill into an API bill.

Why nobody sees the 10 MB minimum​

Any one of these queries looks cheap. Athena reports a few kilobytes scanned and a quick answer, which is what most Athena cost advice tells you to aim for. The minimum is applied on the invoice, so the query statistics never show it.

Test runs hide it too. With one account and a few test tables, the same job sends a modest number of queries and the minimum costs cents. It only adds up in production, once the job runs over every account and every table, and even then the cost is split across two services, neither of which names the job.

How to batch small Athena queries​

Ask one larger question, then let the application split the answer. In this team's SQL, a filter on one value became a column in the result.

SELECT DISTINCT region, {dim_expr} AS dim_value
FROM {athena_db}.{inventory_table}
WHERE account_id = '{account_id}'
AND {dim_expr} IS NOT NULL

The query no longer filters on region, so it returns every region at once and the caller reads its slice from a cached result. Two lines of SQL changed. With the code that reuses the answer, the whole fix is 86 lines outside tests.

The next day's bill showed the result. These are the medians of the five weekdays on each side of the change:

Weekday medianBeforeAfterChange
Billed Athena queries810,00053,000÷15
Athena cost$65.45$20.00−$45.45
S3 GET cost$59.53$7.32−$52.21

That is $98 a weekday, or about $25,000 a year at that rate.

Batching isn't free. The cached answer is only as fresh as its last refresh, one failed query now fails for every region, and the first caller waits longer than before. A job that only lists which items exist can usually live with that.

Two more ways to cut Athena query cost​

A view is a saved query, so everyone who reads it runs the full scan again. On the same account, one model had no materialization setting. It took the project default, a view, and every mart that joined it paid for a 60.66 GB scan per rebuild. Stored as a table, it costs one 60.66 GB scan a night, and each mart then reads it for 0 GB.

Typed columns help too. Reading a name out of a base64 JSON blob scanned 503 MiB, against 41 MiB for the same value read from a column of its own.

Catching it after each deploy​

Three numbers give this pattern away: queries per day, queries under 10 MB per day, and S3 request cost against storage cost. All three come from a daily read of the bill and the runtime metrics. If the count of queries under 10 MB jumps, a change has started paying the minimum, and the next day's count tells you whether the fix worked.

A software factory (a set of agents that write, test and ship changes) can make that check after each deploy, the way it runs a unit test. Did the change multiply the queries per run? The test suite can't answer that, because the cost only appears at production volume.

Cloud ex Machina runs and prices this cost feedback loop for software factories. It reads the bill and the runtime metrics every day, ties a step in cost to the change and the job behind it, and measures the next bills after the fix. Teams without a factory get the same findings as application-layer FinOps recommendations, each with its saving and the team that owns the code.

Three checks to run on your own account​

  • Count the successful DML queries under 10 MB (DataScannedInBytes) in the history of each Athena workgroup. Each one pays the minimum. DDL, partition statements and failed queries cost nothing. Neither do queries served from reused results, and a provisioned-capacity workgroup bills by DPU-hour, with no minimum.
  • Compare S3 request cost with storage cost for each bucket. If requests cost more than storage, look at file sizes and at who reads them.
  • Look for a query inside a loop. One query per region, per date or per account is the pattern to batch.

FAQ: Athena minimum charge and small queries​

What is the minimum charge for an Athena query?​

Athena bills SQL queries by the bytes they scan, rounded up to the next megabyte, with a 10 MB minimum per query. At $5 per terabyte, a query that reads 38 KB costs the same as a query that reads 10 MB.

Why can S3 GET requests cost more than the storage?​

S3 bills each GET request, whatever the size of the object, so the request bill follows the file count and how often the files are read. On the account in this story, one month of GET requests cost $1,848 and the storage cost $15.

How do I find Athena queries that pay the 10 MB minimum?​

Count the successful DML queries whose DataScannedInBytes is under 10 MB in the query history of each workgroup. Each one pays the minimum, whatever it reads. DDL, partition statements and failed queries cost nothing. Neither do queries served from reused results, and a provisioned-capacity workgroup bills by DPU-hour, with no minimum. Then look for the code that sends one query per region, per date or per account, and batch it.