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
Last updated
I got a $134 Cloudflare D1 bill. Here's how I cut it 95%.
Open original sourceJustin 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.
Read originalArchived
This archive does not contain the complete text.
I got a $134 Cloudflare D1 bill. Here's how I cut it 95%.
On April 21, 2026, my Cloudflare invoice was $134.14.
More to read
sean1093/purse-strings: in-process LLM spend budgets, with optional Durable Objects or KV stores
The MIT-licensed TypeScript library purse-strings (sean1093/purse-strings) is described as a denial-of-wallet guard for apps that call LLM APIs: per-user, per-session and global token, cost and request budgets, plus step, tool-call and time limits for agent loops. It has zero runtime dependencies and can keep counters in memory, Firestore, or Cloudflare Durable Objects or Workers KV. The README warns that with multiple process replicas, each replica enforces the limit, so the effective limit is N times the configured value unless a shared store is used, and that a Durable Objects or KV store is what a hard limit needs. Reviewed from the public README on 2026-11-19. Example figures in the docs include a user-daily cap of costUsd: 1 and a fleet-daily cap of costUsd: 200.
Revulate: a usual $5–10 bill became $2,580.48 from Durable Objects; $2,080.48 offered as service credit.
Revulate (@Revulate) says their usual $5–10 bill became $2,580.48 from accidental Durable Objects usage. Support approved $2,080.48 in future service credit under case 02382870, but the author says they need a refund to the original PayPal payment and asks Cloudflare staff for help. The author later says a follow-up case #02390382 was closed by the same agent, and that another reviewer and Finance declined the PayPal refund.
runes_leo: applying for the Cloudflare for Startups $10k bootstrapped tier with an AI agent
@runes_leo describes applying for the Cloudflare for Startups $10k tier without funding: the three tiers, who qualifies, the four-page form, and three pitfalls (a sign-in button greyed out in Chrome, a required company-domain email, and a payment method required on the linked account). He later corrected the $100k/$350k criteria, said approval and credits were still pending, and on November 10 updated that Workers AI is no longer covered for new members (existing $10k-tier members up to $2,500 until December 1).