Skip to content

perf: add database indexes on frequently queried columns - #79

Merged
Cjay-Cyber-2 merged 4 commits into
mergepay:mainfrom
Fayyo:feat/query-indexes
Jul 31, 2026
Merged

perf: add database indexes on frequently queried columns#79
Cjay-Cyber-2 merged 4 commits into
mergepay:mainfrom
Fayyo:feat/query-indexes

Conversation

@Fayyo

@Fayyo Fayyo commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Summary

Add database indexes on frequently queried columns to improve query performance as the dataset grows. Indexes target the most common query patterns identified in route handlers: listing expenses by group (sorted by date), fetching settlements ordered by date, and retrieving audit logs by entity.

Changes

New indexes added to prisma/schema.prisma

Model Index Columns Purpose
Expense @@index([groupId, createdAt]) group_id, created_at Composite index covering the primary list query findMany({ where: { groupId }, orderBy: { createdAt: "desc" } }) used in expense listing, ledger, and balance computation
Settlement @@index([createdAt]) created_at Supports ordering by date in history (GET /history) and worker FIFO processing
AuditLog @@index([entityType, entityId]) entity_type, entity_id Composite index for filtering audit logs by entity type and ID
AuditLog @@index([createdAt]) created_at Supports ordering audit logs by timestamp

Existing indexes that already covered other AC requirements

  • GroupMember: @@index([userId]) and @@unique([groupId, userId]) (composite B-tree covers groupId, userId lookups) ✅
  • Expense: @@index([groupId]), @@index([payerUserId])
  • Settlement: @@index([groupId]), @@index([status]), @@index([fromUserId]), @@index([toUserId])
  • AuditLog: @@index([userId])

Migration

Generated migration at prisma/migrations/20260728000001_add_query_indexes/migration.sql:

Index SQL
Expense group+date CREATE INDEX "expenses_group_id_created_at_idx" ON "expenses"("group_id", "created_at");
Settlement date CREATE INDEX "settlements_created_at_idx" ON "settlements"("created_at");
AuditLog entity CREATE INDEX "audit_logs_entity_type_entity_id_idx" ON "audit_logs"("entity_type", "entity_id");
AuditLog date CREATE INDEX "audit_logs_created_at_idx" ON "audit_logs"("created_at");

Note on over-indexing

Expense does not have a status field (status exists on ExpenseShare, not Expense itself), so the suggested @@index([status]) was omitted to avoid referencing a non-existent column.

Testing

  • ✅ All 79 tests across 11 test files pass with no regression
  • ✅ Indexes verified present in database via \di
  • ✅ Migration is non-destructive and safe for production (no downtime)

Closes #35

@drips-wave

drips-wave Bot commented Jul 28, 2026

Copy link
Copy Markdown

@Fayyo Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@Cjay-Cyber-2
Cjay-Cyber-2 merged commit d555c2f into mergepay:main Jul 31, 2026
1 check failed
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.

Add database indexes on frequently queried columns to improve query performance

2 participants