Every row SQLite reads is on the bill

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.

The trap: a snapshot taken at the wrong moment

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.

Three ways to keep plans in line, and what each costs on Cloudflare

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.

Two valid answers

PRAGMA optimize and INDEXED BY both fixed the trap in our tests. Neither is wrong. They trade different things:

PRAGMA optimizeINDEXED BY
Who picks the indexSQLite, from statisticsYou, in the query
Matches SQLite's adviceYesNo; they discourage it for tuning
Plans depend on when statistics were takenYes; it stayed correct in our test, and we didn't test long-running churnNo
Missing indexQuietly scansFails with an error
Ongoing workRun it periodically and after schema changesDeclare the right indexes up front
Billed cost on CloudflareA handful of rows per run (observed; may change)Nothing extra
Works for every queryYesOnly 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:

  • A read budget per request. Every request has a ceiling on rows read (100,000 in production). Once a request crosses it, its remaining reads are stopped and flagged.
  • Current-copy indexes. Every index has a twin that covers only current rows, so old copies stay out of everyday reads.

One more caveat: the reproduction is controlled. It shows the risk; it isn't a replay of our bill.

If you run SQLite somewhere you pay per read

  • Check EXPLAIN QUERY PLAN on your hot queries. A SCAN on a big table is a line item.
  • If you collect statistics on Durable Objects, use PRAGMA optimize, not a plain ANALYZE.
  • Look at your plans again after statistics are taken at a quiet moment, not just on test data.
  • Pick one: keep statistics fresh with PRAGMA optimize, or name your indexes and stop depending on statistics.