P
Prisma3w ago
CeeJay

Cloudflare D1 Performance

I'm doing a straight forward query of a small DB from a cloudflare worker (with Hono) using Prisma. Locally the query take 8ms (measured from worker), but on the cloud the query takes between 200ms to over 1s.
start = performance.now()
if (c.get('caller') !== 'worker') {
return c.json({ error: 'Unauthorized caller' }, 401)
}
const clip = await prisma.videoClip.findFirst({
where: {
status: 'waiting'
},
orderBy: {
createdAt: 'desc'
}
});

let end = performance.now()
console.log(`PERFLOG [JOB NEXT RETRIEVE]: ${c.req.method} ${c.req.url} ${end - start}ms`)
start = performance.now()
if (c.get('caller') !== 'worker') {
return c.json({ error: 'Unauthorized caller' }, 401)
}
const clip = await prisma.videoClip.findFirst({
where: {
status: 'waiting'
},
orderBy: {
createdAt: 'desc'
}
});

let end = performance.now()
console.log(`PERFLOG [JOB NEXT RETRIEVE]: ${c.req.method} ${c.req.url} ${end - start}ms`)
Cloudflare D1 reports that the queries are taking less than 1ms. Also I think the calls are generating lots of cpuTime which is adversely affecting my service given it routinely exceeds the limits. There are two issues: the latency and the (reported) CPU consumption - how can I further diagnose / remedy?
3 Replies
Prisma AI Help
You're in no rush, so we'll let a dev step in. Enjoy your coffee, or drop into #ask-ai if you get antsy for a second opinion!
CeeJay
CeeJayOP3w ago
Okay more context / information. I have added additional mechanisms to query the DB using prisma.$queryRawUnsafe and c.env.DB.exec to observe differences in response time. Interestingly the response time for each are broadly similar at ~200ms, BUT the cpuTime for the request is fairly static at ~180ms. I've tried combinations of the client-adapters, the 'native' c.env.DB.exec gives a variable time of between 5-90ms. So in summary it would appear there is something fishy with the D1 service occassionally taking significantly more time (recognising I have done fairly limited test iterations). However it's equally clear that the prisma-d1-adapter is consistantly taking around 200ms meaning it's unsuitable for CF workers given the cpuTime limits. I'd be very happy to be wrong or have a misconfigured prisma client, but looks like I'll need to take it out...
RaphaelEtim
RaphaelEtim3w ago
Hi @CeeJay Can you enable tracing to get more detailed information about where time is being spent during query execution?
OpenTelemetry tracing | Prisma Documentation
Diagnose application performance with detailed traces of each query.

Did you find this page helpful?