billflare.

Cutting a D1 bill 95% with composite indexes, ANALYZE and a KV cache

Justin Ahinon walks through finding the four queries behind 127.6 billion D1 rows read and fixing them with indexes, ANALYZE and cache-aside KV for layout-level data.

Products
D1KVWorkers
Source
Author experienceFull Stack SvelteKit
Author
Justin Ahinon

Key points

  • He ranked queries with the D1 dashboard and wrangler d1 insights. Four unindexed full-table scans, two of them in a layout loader that ran on every page, caused 93% of reads on a 765,000-row table.
  • Fixes: four composite indexes matching the filter and group-by patterns, then ANALYZE (or PRAGMA optimize) so the planner picks the right index; that took one aggregation from about 1 million rows to 150,572.
  • He then wrapped data that changes once a year in cache-aside KV with versioned keys and manual invalidation, and projects 30-day reads falling from about 400 billion to 19 billion rows from indexes alone.
  • Official D1 docs (rechecked 2026-10-14): rows read count every row scanned, not returned; Workers Paid includes 25 billion a month, then $0.001 per million; the indexes guide recommends PRAGMA optimize after creating indexes.

Original sources

  1. Last updated

    Cutting a D1 bill 95% with composite indexes, ANALYZE and a KV cache

    Open original source

    Justin Ahinon walks through finding the four queries behind 127.6 billion D1 rows read and fixing them with indexes, ANALYZE and cache-aside KV for layout-level data.

Author experienceWorkers +4

After a $10,000 loop bill: budget alerts and a cost-audit prompt for vibe-coded projects

铁柱AGI (@cgnot996) writes an X article that walks through creating a Cloudflare budget alert, explains why it is only a notice, lists cost-amplifying code patterns, and gives a read-only audit prompt and steps for when a bill has already spiked.

Other

sylwair: nearly $900 of usage in under two days after a project mistake.

@sylwair says a recent project mistake ran up nearly $900 in less than two days. He says he spotted it in Cloudflare's billable-usage view and stopped the abnormal service, and in a reply adds that the daily billing figures appeared with a delay, so the amount did not visibly drop after he stopped it.

Author experienceDurable Objects +3

Designing a poker game on Durable Objects so idle time costs nothing

Philip Atkinson describes the architecture of his multiplayer poker game on the Cloudflare Developer Platform and the cost rules he follows: archive finished games out of Durable Object storage, hibernate WebSockets, wake with alarms and treat logging as a cost decision.