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.
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
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!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...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.