Cloudflare D1 reads too high? Check scans and cache hits
gist Measure rows_read for each query, add up the work per request, and inspect the cache hit branch for database calls.
You stopped about in. Jump back
contents
If a small Cloudflare D1 app reads many rows with few visitors, measure query scans and frequency, then check whether cache hits still query the database.
I use Workers, D1 and Queues. For this check, have your Worker code, database schema and usage metrics available. Start with one endpoint whose read usage looks high.
Step 1: Separate requests from rows read
| Count | What it tells you |
|---|---|
| Worker requests | How often the Worker handles a request |
| D1 queries | How many database queries execute |
| D1 rows read | How many rows those queries scan |
One request can run several queries. A query can return one row after scanning many rows. D1’s read-query count is separate from its rows-read billing metric. D1 metrics documentation
Check the cache first and return the cached response on a hit. Put the D1 query in the miss branch, then check whether other code starts database work before that branch.
For each request, add the rows_read values from every query it executes. Include queries from scheduled jobs, queue consumers and retries when counting daily usage. Compare the same time window across all databases in the account. D1 billing metrics
Step 2: Record the work each query does
Use the metadata returned by the Workers binding. This uses a binding named DB and a table named public_items with id, title and category columns. Adapt it to your schema.
async function listPublicItems(env, category) {
const result = await env.DB.prepare(
"SELECT id, title FROM public_items WHERE category = ? LIMIT 20"
)
.bind(category)
.all();
console.log({
query: "items_by_category",
rows_read: result.meta.rows_read,
rows_written: result.meta.rows_written,
rows_returned: result.results.length,
});
return result.results;
}
Use bound parameters for values. Keep logs to fixed query labels and counts. Leave out parameter values, returned records and personal data.
Use .all() when inspecting metadata. .first() returns a row or column without metadata and does not add a SQL limit. Prepared statement documentation
Over the same time window, group records by query label. Compare total rows read, execution count and rows returned. Prioritize queries with the highest total read usage. D1 metrics and query insights can help find them. D1 metrics and analytics
Step 3: Check scans and indexes
Inspect the query with EXPLAIN QUERY PLAN. Look for SCAN versus SEARCH ... USING INDEX. A scan is fine for a query that needs most rows; elsewhere it is usually the cost.
For this query, consider an index on category. Check filter, join and ordering columns in other queries. Compare meta.rows_read before and after using the same query and data.
Create indexes once in schema migrations. They use storage and add writes when indexed values change.
Watch these patterns:
ORDER BY RANDOM() LIMIT 1reads and sorts the result set to choose one row. The limit does not make that work a single-row read.LIKE '%term%'cannot use a regular B-tree index to optimize the leading-wildcard search.
Both patterns appear in Cloudflare’s D1 index guidance. Verify the plan and row counts before choosing a change.
Step 4: Check whether cache hits skip D1
cache.match() returns a cached response or undefined. Missing or expired entries return undefined. The Cache API does not decide which application code runs afterward. Workers Cache API documentation
Return a usable cached response before any database work starts. Returning skips the statements after it; it can’t cancel a query that already started. Inspect middleware, background work and refresh logic too.
Check:
- Key: Use the same key for lookup and storage. Keep parameters that change the response.
- TTL: Choose how long stale data is acceptable. Expired entries cause misses.
- Location: Cache API entries do not replicate outside their originating data center.
- Visibility: Cache only public responses suitable for sharing. Keep personalized or authenticated responses out of the public cache.
The Cache API documentation explains lookup keys, expiry and locality.
Record cache status alongside query counts. Investigate any hit that also executes a D1 query. Saving a response after querying cannot remove reads already incurred.
Step 5: Choose what to change
| Option | When it fits | Tradeoff |
|---|---|---|
| Better indexes | Frequent queries scan far more rows than they return | Extra storage and writes; verify the plan |
| Application caching | Public responses can tolerate stale data | Keys, expiry and invalidation need care |
| Workers Paid with D1 | Useful, optimized work exceeds the free allowance | More included usage does not reduce scans |
| Postgres | The workload needs a different database approach | Migration, hosting and query tuning |
Start with the query labels that account for the most reads. Check their plans and cache paths. Then estimate the cost of the remaining work.
As checked on October 10, 2026, Workers Free includes 5 million D1 rows read per day. Workers Paid includes monthly usage, with charges above it. D1 pricing
On the free plan, queries fail after the daily read or write allowance is reached. They can run again when the allowance resets at midnight UTC. Check current pricing before changing plans. D1 daily limit documentation
Common questions
Why are D1 reads higher than my page views?
Page views count visits, while D1 counts scanned rows. One request can run several queries. Scheduled jobs and queue consumers can query D1 without a page view. D1 metrics documentation
Does LIMIT 1 mean D1 reads one row?
No. It limits returned rows, not scanned rows. ORDER BY RANDOM() LIMIT 1 still reads and sorts the result set. Check meta.rows_read and the query plan. D1 index guidance
Why does D1 still run when I cache responses?
Check for a miss, expiry or a different key. Cache API contents also differ by data center. Then inspect whether the handler starts a query before checking the cache or refreshes data on a hit. Cache API documentation
Does batching queries reduce rows read?
Batching alone does not remove each statement’s database work. Inspect and add up the row counts for the statements in the batch. D1 counts query usage as rows read, rows written or both. D1 pricing
Will a paid plan fix an inefficient query?
No. It changes the allowance and billing terms. Reduce avoidable scans before estimating paid usage. D1 pricing
Related: Cloudflare guides · guide index.
../cloudflare/ every Cloudflare guide