How a missing index can burn your Cloudflare D1 read quota
D1 bills and limits by rows read, not queries. A full table scan on every request adds up fast. Some math, and how to catch it.
D1 bills rows read and written, not queries. That sounds like a detail until a query without an index scans the whole table on every request.
Say you have a 1M row table and a lookup on a column with no index. Each call reads 1M rows. At 10 requests per second, that's 864,000 calls a day, or about 864 billion rows read. At $0.001 per million rows that's around $864 a day at list price, before the Paid plan's 25 billion rows a month allowance. For one missing index. Wild.
On the Free plan it fails differently. The daily read limit is account-wide, so one bad Worker eats the quota and D1 breaks for every other Worker in the account. Your bill stays at $0 and everything is down, which is maybe worse.
How to catch it
- Run
EXPLAIN QUERY PLANon your hot queries. If you seeSCANinstead ofSEARCH ... USING INDEX, that's the one. - D1 returns
meta.rows_readwith each result. Log it for a day, the outliers jump out. - Watch rows read per database in the D1 analytics, not just query count.
workerfuse can pause a Worker once reads across its bound D1 databases pass a daily cap. I added that mostly for the Free plan case, where your invoice stays at $0 but the quota is what actually hurts. Fair warning: it checks every five minutes, so a scan loop this fast can still eat the free quota first. And a shared database counts against every script bound to it.