Skip to content

perf: add database indexes for invoice queries (#259)#276

Open
Tijesunimi004 wants to merge 2 commits intodavedumto:mainfrom
Tijesunimi004:feat/issue-259-add-performance-indexes
Open

perf: add database indexes for invoice queries (#259)#276
Tijesunimi004 wants to merge 2 commits intodavedumto:mainfrom
Tijesunimi004:feat/issue-259-add-performance-indexes

Conversation

@Tijesunimi004
Copy link

  • Add 5 composite/single-column indexes to Invoice model: [userId, status, createdAt DESC], [userId, paidAt DESC], [clientEmail, status], [dueDate], [status, createdAt DESC]

  • Add missing indexes to Transaction: [userId, status, createdAt DESC], [invoiceId], [type, status]

  • Add [status, createdAt DESC] index to Dispute

  • Add [userId, status] composite index to PaymentAdvance

  • Add [invoiceId, createdAt DESC] and [actorId] indexes to AuditEvent

  • Create migration: 20260226000000_add_invoice_performance_indexes

  • Update lib/db.ts: emit query events and log slow queries >100ms

  • Add tests/performance/invoice-queries.test.ts with <50ms assertions for all indexed query patterns across Invoice, Transaction, Dispute, AuditEvent, and PaymentAdvance

Closes #259

- Add 5 composite/single-column indexes to Invoice model:
  [userId, status, createdAt DESC], [userId, paidAt DESC],
  [clientEmail, status], [dueDate], [status, createdAt DESC]

- Add missing indexes to Transaction:
  [userId, status, createdAt DESC], [invoiceId], [type, status]

- Add [status, createdAt DESC] index to Dispute

- Add [userId, status] composite index to PaymentAdvance

- Add [invoiceId, createdAt DESC] and [actorId] indexes to AuditEvent

- Create migration: 20260226000000_add_invoice_performance_indexes

- Update lib/db.ts: emit query events and log slow queries >100ms

- Add tests/performance/invoice-queries.test.ts with <50ms assertions
  for all indexed query patterns across Invoice, Transaction,
  Dispute, AuditEvent, and PaymentAdvance

Closes davedumto#259
Copilot AI review requested due to automatic review settings February 26, 2026 12:16
@vercel
Copy link

vercel bot commented Feb 26, 2026

@Tijesunimi004 is attempting to deploy a commit to the david's projects Team on Vercel.

A member of the Team first needs to authorize it.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds database performance optimizations through strategic indexing to address slow query performance for invoice-related operations. The changes implement the solution described in issue #259, which identified that invoice list queries were taking 800ms-2.1s due to missing indexes, with the goal of achieving 67x faster query performance.

Changes:

  • Added 11 new database indexes across Invoice, Transaction, Dispute, PaymentAdvance, and AuditEvent models to optimize common query patterns
  • Enhanced database client configuration to emit query events and log slow queries exceeding 100ms
  • Added performance test suite to validate sub-50ms query times for indexed operations

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
prisma/schema.prisma Added 11 new composite and single-column indexes across 5 models (Invoice, Transaction, Dispute, PaymentAdvance, AuditEvent) to optimize filtering and sorting operations
prisma/migrations/20260226000000_add_invoice_performance_indexes/migration.sql Migration SQL to create all 11 indexes with descriptive comments explaining each index's purpose
lib/db.ts Modified PrismaClient configuration to emit query events and log slow queries over 100ms threshold
tests/performance/invoice-queries.test.ts New performance test suite with 50ms threshold assertions for all indexed query patterns
Comments suppressed due to low confidence (3)

prisma/schema.prisma:467

  • Redundant index detected. The new composite index [invoiceId, createdAt(sort: Desc)] at line 465 makes the existing single-column index [invoiceId] at line 467 redundant. The composite index can handle queries that filter only by invoiceId since invoiceId is the leftmost column. Consider removing the @@index([invoiceId]) at line 467 to avoid index maintenance overhead and storage waste.
  @@index([invoiceId, createdAt(sort: Desc)])
  @@index([actorId])
  @@index([invoiceId])

prisma/schema.prisma:449

  • Redundant index detected. The new composite index [userId, status] at line 448 makes the existing single-column index [userId] at line 449 redundant. The composite index can handle queries that filter only by userId since userId is the leftmost column. Consider removing the @@index([userId]) at line 449 to avoid index maintenance overhead and storage waste.
  @@index([userId, status])
  @@index([userId])

lib/db.ts:15

  • Query event logging is enabled in all environments including production, which could impact performance and generate excessive logs. The previous implementation only logged queries in development mode. Consider conditionally enabling query event emission based on environment, or at minimum only log slow queries in production while keeping full query logging for development.
    log: [
      { level: 'query', emit: 'event' },
      { level: 'warn', emit: 'stdout' },
      { level: 'error', emit: 'stdout' },
    ],
  })

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
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.

Performance: Add database indexes for invoice queries (67x faster)

2 participants