Author
October 19, 2025
2 min read

D1 brings the familiarity of SQLite to Cloudflare's edge network, but serverless SQLite operates under different constraints than a local database. Network latency between the Worker and the D1 instance—not query complexity—is the primary bottleneck. Optimizing for this changes how you write queries.
Every D1 query travels from your Worker to the nearest D1 instance over Cloudflare's internal network. This round trip adds 5–15ms per query. For a page that makes six individual queries, that overhead accumulates to 30–90ms of pure transport time before any query executes.
The solution is batching. D1's DB.batch() method accepts an array of prepared statements and sends them in a single HTTP request. The database executes them sequentially and returns all results together. A page that previously made six round trips now makes one.
D1 uses SQLite's query planner, which means standard B-tree indexing rules apply, but with a serverless twist. Because each query incurs network overhead, the cost of a full table scan is proportionally higher relative to the query execution time. Indexing becomes more valuable, not less.
Index columns used in WHERE clauses, JOIN conditions, and ORDER BY expressions. D1 supports covering indexes that include all columns needed by a query, which can eliminate table lookups entirely. Use EXPLAIN QUERY PLAN to verify the planner is using your indexes.
Every call to DB.prepare() with the same SQL text allows D1 to reuse the cached query plan. This is especially important for frequently executed queries in high-traffic applications. Combined with parameterized binds, prepared statements also prevent SQL injection and ensure that user input is correctly escaped.
Cloudflare has announced global read replicas for D1, which will allow read-only queries to execute at the nearest edge location while writes continue to hit the primary instance. Designing your application with eventual consistency tolerance now—using KV or cache layers for hot data—means you will be ready to enable replicas with zero code changes when they roll out.
The key insight: serverless SQL performance is less about SQL optimization and more about reducing network round trips. Batch aggressively, index strategically, and prepare for a distributed read path.