D1 adding more latency than expected

Im using sveltekit and i created 2 test routes. they essentially do the same but one does a select 1; in d1. When warm the the difference in response time is 50ms which just seems super high considering the query takes less than 1ms. I chose the cloest location to me when i created d1 I use workers with assets. here the code for both api endpoints i used to just test it out Is this really the expected behaviour? No Db:
import type { RequestHandler } from './$types';

export const GET: RequestHandler = async () => {
const uuid = crypto.randomUUID();
return new Response(uuid);
};
import type { RequestHandler } from './$types';

export const GET: RequestHandler = async () => {
const uuid = crypto.randomUUID();
return new Response(uuid);
};
With D1:
import { drizzle } from 'drizzle-orm/d1';
import { sql } from 'drizzle-orm';
import type { RequestHandler } from './$types';

export const GET: RequestHandler = async ({ platform }) => {
const db = drizzle(platform!.env.DB);
await db.run(sql`SELECT 1`);
const uuid = crypto.randomUUID();
return new Response(uuid);
};
import { drizzle } from 'drizzle-orm/d1';
import { sql } from 'drizzle-orm';
import type { RequestHandler } from './$types';

export const GET: RequestHandler = async ({ platform }) => {
const db = drizzle(platform!.env.DB);
await db.run(sql`SELECT 1`);
const uuid = crypto.randomUUID();
return new Response(uuid);
};
i tried without drizzle as well but i dont see a difference
4 Replies
Silvan
SilvanOP4d ago
Is there a good way to like trace the entire request and see all latencies or something?
Chaika
Chaika4d ago
https://developers.cloudflare.com/workers/runtime-apis/performance/ can perf.now before/after the sql and see how much latency it's adding directly
Cloudflare Docs
Performance and timers · Cloudflare Workers docs
Measure timing, performance, and timing of subrequests and other operations.
Chaika
Chaika4d ago
50ms doesn't sound too crazy though, your db is same region but not same data center as the one you're hitting, more then likely
Silvan
SilvanOP4d ago
ye its very consistently 40ms. and yea i wasnt considering the "same data center" fair enough

Did you find this page helpful?