D1 Querys slow

For Workes & Pages, what is the name of the domain?

https://api.aaron-schewtschenko.workers.dev/query

What is the issue or error you’re encountering

Response times are slow, sometimes 1500ms constantly 200-250ms. When visiting /hello workers response with response times are around 20ms.

What steps have you taken to resolve the issue?

Using VPN for different connections
Different Queries
Different Locations for D1 Database

What are the steps to reproduce the issue?

Request URL and view response times in Perth Australia.

Depending on he complexity of your data/query, this could be totally normal.

Can you share a way to reproduce this with some data and a query?

These are rows up to 5:

What my Query does it grab 20 rows of data and they all very similar in length.
All the data together only comes to 7kb but these are the times from the D1 console:
image

Query:
SELECT * FROM events;
Database only has 20 rows

Here is also the median wall time for when the api exacutes the Query

Hi, i have same issue with d1 response it’s very slow, run 3 query client response in 1.34s

This is my worker code

export default {
	async fetch(request, env, ctx) {

		/** @type {import('@cloudflare/workers-types/experimental').D1Database} */
		let db = env.DB

		let now = Date.now()
		let a = await db.prepare('SELECT CURRENT_TIMESTAMP').all()
		console.log(`A elapsed: ${Date.now() - now}, query duration: ${a.meta.duration}`)
		
		now = Date.now()
		let b = await db.prepare('SELECT CURRENT_TIMESTAMP').all()
		console.log(`B elapsed: ${Date.now() - now}, query duration: ${b.meta.duration}`)

		now = Date.now()
		let c = await db.prepare('SELECT CURRENT_TIMESTAMP').all()
		console.log(`C elapsed: ${Date.now() - now}, query duration: ${c.meta.duration}`)

		return new Response(c.results[0].CURRENT_TIMESTAMP)
	}
}

This is from cloudflare Real-time logs

{
  "message": [
    "A elapsed: 737, query duration: 0.1337"
  ],
  "level": "log",
  "timestamp": 1730291663216
},
{
  "message": [
    "B elapsed: 304, query duration: 0.1478"
  ],
  "level": "log",
  "timestamp": 1730291663520
},
{
  "message": [
    "C elapsed: 303, query duration: 0.2395"
  ],
  "level": "log",
  "timestamp": 1730291663823
}

For people who are having similar issues.

What I worked out was the worker which was located Perth, Australia but the database was in Sydney, Australia which has 50ms there and back each time. Utilizing the smart placement Smart Placement · Cloudflare Workers docs made the response from 200-250 1500ms sometimes, with smart placement turned off response times went to 90ms-110ms with a high of 300ms. As now the worker and database is together in the same location resulting in much lower ping.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.