Notes
Every row SQLite reads is on the bill
Nov 2, 2026 · Bradley Leatherwood

Notes
Nov 2, 2026 · Bradley Leatherwood


Our agent runtime keeps its working state in SQLite, inside Cloudflare Durable Objects. On that platform you're billed for every row SQLite reads, not just the rows it hands back. A lookup that returns 5 rows but checks 20,000 to find them is billed for 20,000.
We learned what that means the expensive way. For several days our Cloudflare bill ran around $66 a day, almost all of it rows read. Queries were reading every row because no index was being used. The peak day came to $366, mostly rows written.
This note covers what we found when we dug in: how SQLite decides whether to use an index, three standard ways to influence that decision (ANALYZE, PRAGMA optimize and INDEXED BY), what each one actually costs on Cloudflare, and what we chose.
Say you have a support inbox with 20,000 tickets. Almost all are closed and a few are open. To find the open ones fast, you give the database a shortcut: an index, a list of tickets sorted by status. With it, the database jumps straight to the open pile.
Before every search, SQLite's query planner decides which shortcut to use, or whether to use one at all. It can only pick from indexes that exist. If there isn't a suitable one, it reads the whole table and says nothing. That was our first problem, and the easiest to fix: add the right indexes.
When there are several indexes to choose from, the planner can consult statistics about your data, roughly how many rows share each value. The statistics aren't updated as data changes. They're a snapshot, taken when someone runs ANALYZE or PRAGMA optimize. Without a snapshot, SQLite uses built-in guesses. With one, it trusts the snapshot.
To see how much the snapshot matters, we built a reproduction on Cloudflare's actual SQLite engine (workerd, run locally through Miniflare). It writes data through our real data layer into the real collection our agent runtime ships: 20,000 incoming requests that are almost all handled, 8,000 scheduled follow-ups that are almost all finished, and 40,000 transcript entries. Then we ran real queries from our code, such as "which requests are still pending?"
With no snapshot, and with a snapshot taken while work was pending, SQLite chose the status index every time. It read 21 rows to return 20.
Then we took a snapshot (ANALYZE) at a quiet moment, when every request had been handled. The snapshot now said every request has the same status, so the status index doesn't narrow anything down. On average that's true. But the status we always ask for is the rare one, so it's exactly the wrong lesson.
Five new requests arrived. SQLite trusted the snapshot, skipped the status index and checked every request: 20,006 rows read to return 5.
A second query went wrong in a similar way. Our storage keeps older copies of each record for a while, so we can open consistent snapshots and roll back. The quiet-moment statistics made an index that also covers those old copies look like the best choice. SQLite read 8,000 old copies to return 3 current rows.
We measured each option on a Durable Object deployed to our own account. We seeded four objects identically (about 37,000 rows each), ran one maintenance step per minute, and read the billed rows from Cloudflare's per-minute analytics. The billed numbers matched what SQLite's own counters reported, row for row.
ANALYZE takes a full statistics snapshot right now. On Cloudflare it was billed for everything it read: about 2 million rows for a 37,000-row object, roughly 53 reads per stored row. It's also what produced the bad plan above, because it captures whatever the data looks like at that moment.
PRAGMA optimize is what SQLite actually recommends. Run it periodically and after creating indexes, and it re-analyzes a table only when it thinks the statistics are missing or stale. Cloudflare's D1 guide gives the same advice. In our reproduction it never produced the bad plan. A queue draining changes statuses, not the number of rows, so optimize kept the good snapshot from earlier.
It was also surprisingly cheap. Its first run was billed for only a handful of rows. That's what we observed on 2026-10-28, not published pricing, so it could change. If you keep statistics on Durable Objects, use optimize, never a plain ANALYZE.
Adding an index has its own cost: building it was billed at about one read per stored row, plus a write for every row it covers. Afterwards, PRAGMA optimize re-analyzed the table, again billed at a handful of rows.
INDEXED BY names the index inside the query itself, so the planner has nothing to decide. If that index is missing, SQLite fails with an error instead of falling back to a scan. (It can still read all of a named index that doesn't fit the query, so check the plan.) In our reproduction it read the minimum in every condition: 6 rows instead of 20,006, and 4 instead of 8,000. It needs no statistics, so it adds no maintenance cost.
SQLite's documentation discourages it for tuning: it says INDEXED BY "is not intended for use in tuning the performance of a query." It describes it as a way to lock a plan down and get an error when that plan would change.
PRAGMA optimize and INDEXED BY both fixed the trap in our tests. Neither is wrong. They trade different things:
PRAGMA optimize | INDEXED BY | |
|---|---|---|
| Who picks the index | SQLite, from statistics | You, in the query |
| Matches SQLite's advice | Yes | No; they discourage it for tuning |
| Plans depend on when statistics were taken | Yes; it stayed correct in our test, and we didn't test long-running churn | No |
| Missing index | Quietly scans | Fails with an error |
| Ongoing work | Run it periodically and after schema changes | Declare the right indexes up front |
| Billed cost on Cloudflare | A handful of rows per run (observed; may change) | Nothing extra |
| Works for every query | Yes | Only queries that map to one declared index; OR, IN, search and aggregates still go to the planner |
We went with INDEXED BY for the queries our data layer can map to a declared index. It already knows, from how each kind of record is defined, which index a query should use, so writing INDEXED BY into the SQL costs us nothing extra. What decided it was the failure mode: after paying for days of full scans, we wanted a missing index to be an error in development, not a line on the bill.
If your team would rather follow SQLite's guidance and keep statistics fresh, PRAGMA optimize is a reasonable choice. On Durable Objects it's cheap today. Just never use a plain ANALYZE there. For our own queries that can't be pinned, it may well be the right tool; we haven't measured that yet.
Two safeguards sit behind either choice:
One more caveat: the reproduction is controlled. It shows the risk; it isn't a replay of our bill.
EXPLAIN QUERY PLAN on your hot queries. A SCAN on a big table is a line item.PRAGMA optimize, not a plain ANALYZE.PRAGMA optimize, or name your indexes and stop depending on statistics.