Skip to content

Data processing optimizations: eliminate N+1 queries and add performance indexes - #1

Draft
lancepan123 with Copilot wants to merge 9 commits into
mainfrom
copilot/data-processing-optimization
Draft

Data processing optimizations: eliminate N+1 queries and add performance indexes#1
lancepan123 with Copilot wants to merge 9 commits into
mainfrom
copilot/data-processing-optimization

Conversation

Copilot AI commented Feb 14, 2026

Copy link
Copy Markdown

Multiple query inefficiencies were causing poor performance at scale: N+1 patterns in order creation, in-memory aggregations, missing pagination, and absent indexes on search/join columns.

Query Pattern Fixes

Order creation N+1 → batch operations

// Before: 1 + 2N queries
for (const item of items) {
  const prod = await db.select().from(products).where(eq(products.id, item.productId));
  await db.insert(orderItems).values({...});
}

// After: 3 queries total
const productsList = await db.select().from(products).where(inArray(products.id, productIds));
const productsMap = new Map(productsList.map(p => [p.id, p]));
await db.insert(orderItems).values(orderItemsValues);

AI model stats: in-memory filtering → SQL aggregation

  • Replaced SELECT * + JS filter with COUNT() and conditional SUM()
  • Eliminates dataset transfer and O(n) client-side processing

Wallet operations: eliminated redundant SELECTs

  • Removed duplicate getWalletByUserId calls in update paths
  • 4-5 queries → 2-3 queries per operation

Users count: conditional join

  • Count query only joins roles table when role filter present

Pagination

Added to getOrders (limit: 20) and getAdminLogs (limit: 50) to prevent unbounded result sets.

Database Indexes

Generated migration 0010_little_exodus.sql with 19 indexes:

  • Search fields: name, username
  • Foreign keys: user_id, role_id, admin_id, order_id, product_id
  • Sort fields: created_at
  • Composite: (status, created_at), (user_id, status)

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 8 commits February 14, 2026 22:01
Co-authored-by: lancepan123 <113438451+lancepan123@users.noreply.github.com>
Co-authored-by: lancepan123 <113438451+lancepan123@users.noreply.github.com>
Co-authored-by: lancepan123 <113438451+lancepan123@users.noreply.github.com>
Co-authored-by: lancepan123 <113438451+lancepan123@users.noreply.github.com>
Co-authored-by: lancepan123 <113438451+lancepan123@users.noreply.github.com>
Co-authored-by: lancepan123 <113438451+lancepan123@users.noreply.github.com>
…ed values

Co-authored-by: lancepan123 <113438451+lancepan123@users.noreply.github.com>
Co-authored-by: lancepan123 <113438451+lancepan123@users.noreply.github.com>
Copilot AI changed the title [WIP] Update data processing for efficiency improvements Data processing optimizations: eliminate N+1 queries and add performance indexes Feb 14, 2026
Copilot AI requested a review from lancepan123 February 14, 2026 22:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants