Why is the first query on each request so slow?

query: 356.14ms
query2: 6.166ms
query: 51.136ms
query2: 4.249ms
query: 47.58ms
query2: 2.664ms
query: 47.062ms
query2: 2.383ms
query: 51.787ms
query2: 3.194ms
query: 356.14ms
query2: 6.166ms
query: 51.136ms
query2: 4.249ms
query: 47.58ms
query2: 2.664ms
query: 47.062ms
query2: 2.383ms
query: 51.787ms
query2: 3.194ms
On each request in the Nextjs App Router is the first query extremly slow. I connect to the database as follows:
import { Pool } from "pg";
import { drizzle } from "drizzle-orm/node-postgres";
import { env } from "~/env.mjs";

const pool = new Pool({
connectionString: env.DATABASE_URL,
});

export const db = drizzle(pool);
import { Pool } from "pg";
import { drizzle } from "drizzle-orm/node-postgres";
import { env } from "~/env.mjs";

const pool = new Pool({
connectionString: env.DATABASE_URL,
});

export const db = drizzle(pool);
I also tried this in a simple nodejs project without next and there it's works. Does anyone have any idea why this is so?
1 Reply
Keef
Keef2y ago
Assuming the queries are the same thing every invocation. The first one is probably cold start plus query execution time which makes sense since you said without next it’s fine. The follow up query is gonna be making use of caching which is why it’s faster
Want results from more Discord servers?
Add your server