From b1c24af031c04b8ecc74f0a6749f161cdc4881dd Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 3 May 2026 08:20:30 +0000 Subject: [PATCH] perf: Parallelize product pagination queries using Promise.all Parallelized the sequential data fetch and total count database queries in `app/api/products/route.ts` using `Promise.all` to reduce request latency and eliminate an unnecessary N+1 pattern. Logged this Drizzle ORM optimization pattern in `.jules/bolt.md`. Co-authored-by: f4teless <60130665+f4teless@users.noreply.github.com> --- .jules/bolt.md | 3 +++ app/api/products/route.ts | 26 ++++++++++++++------------ 2 files changed, 17 insertions(+), 12 deletions(-) create mode 100644 .jules/bolt.md diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000..47cb3b1 --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2026-05-03 - Parallelizing Query and Count in Pagination +**Learning:** In Drizzle ORM applications like this codebase, executing the paginated data fetch and total count queries sequentially creates an unnecessary N+1 pattern that increases request latency. +**Action:** When implementing paginated endpoints or queries, always batch independent sequential database operations (like data fetch and count) using `Promise.all()` to parallelize execution and minimize round trips. diff --git a/app/api/products/route.ts b/app/api/products/route.ts index 953d709..730c920 100644 --- a/app/api/products/route.ts +++ b/app/api/products/route.ts @@ -58,19 +58,21 @@ export async function GET(req: NextRequest) { conditions.push(eq(products.isFeatured, true)); } - const result = await db - .select() - .from(products) - .where(and(...conditions)) - .orderBy(desc(products.displayOrder), desc(products.createdAt)) - .limit(limit) - .offset(offset); + // Parallelize data fetch and total count queries to reduce latency + const [result, [{ count }]] = await Promise.all([ + db + .select() + .from(products) + .where(and(...conditions)) + .orderBy(desc(products.displayOrder), desc(products.createdAt)) + .limit(limit) + .offset(offset), - // Get total count for pagination - const [{ count }] = await db - .select({ count: sql`count(*)` }) - .from(products) - .where(and(...conditions)); + db + .select({ count: sql`count(*)` }) + .from(products) + .where(and(...conditions)) + ]); return NextResponse.json({ products: result,