Write bounded SQL queries, measure index choices, preserve data integrity and plan database changes with recovery evidence.
The SQL examples use SQLite conventions, with Cloudflare D1 examples labelled by their env.DB API. Tables and fields are illustrative. Test changes against your own schema and data before applying them to a live database.
Choose an entry or use Find on page to look for a term. Each number identifies an educational reference; production checks need their own recorded result.
Arrange a multi-column index around the query's equality filters and ordering needs. Verify the actual plan instead of relying on the column list alone.
Example and documentation
CREATE INDEX notes_owner_created ON notes(owner_id, created_at DESC, id DESC);
Offset pagination is simple but skipping a large prefix can cost more work. Compare it with keyset pagination for deep or frequently changing collections.
Example and documentation
SELECT id, title FROM notes ORDER BY id LIMIT 25 OFFSET 250;
Import in measured batches within the platform's current limits. Track progress so an interrupted import can resume without duplicating already accepted records.
Example and documentation
Batch record: source identifier, input range, applied row count, completion state and retry key.
A failed response may leave uncertainty about whether a write succeeded. Use a unique operation identifier or an equivalent verified contract before retrying writes.
Example and documentation
Store a unique operation_id with the effect; a replay checks that record before applying the operation again.
When read replication is used, choose the freshness needed for a session. First-primary starts from the primary; bookmarks carry a minimum database version.
Example and documentation
const session = env.DB.withSession("first-primary");
const result = await session.prepare("SELECT id, title FROM notes WHERE owner_id = ? LIMIT 25").bind(userId).all();
Cache data only when its freshness and privacy requirements permit it. Include the scope that makes a response unique and define how updates invalidate it.
Example and documentation
Public reference cache: route + content version
Private account data: dedicated private policy
Check current per-query and platform limits before designing an import or large operation. Keep a bounded failure path rather than growing a batch indefinitely.
Example and documentation
Document the platform limit, selected batch size, observed duration and recovery checkpoint.
A backup needs a tested restoration process. Check the available recovery window and verify important records after a restore in an appropriate environment.
Example and documentation
Restore exercise: selected recovery point → isolated validation → record count and critical relationship checks.
Apply an approved deletion rule in bounded batches and check the affected rows. Define recovery and retention requirements before executing the operation.
Example and documentation
DELETE FROM notes WHERE id IN (SELECT id FROM notes WHERE archived_at < ? ORDER BY archived_at, id LIMIT 100);