Skip to content

Database indexing optimization for financial queries#580

Open
mendarb wants to merge 1 commit intorohitdash08:mainfrom
mendarb:feat/db-indexing-128
Open

Database indexing optimization for financial queries#580
mendarb wants to merge 1 commit intorohitdash08:mainfrom
mendarb:feat/db-indexing-128

Conversation

@mendarb
Copy link

@mendarb mendarb commented Mar 20, 2026

Summary

  • Adds single-column and composite database indexes to all tables based on analysis of query patterns across expenses, dashboard, bills, reminders, and insights routes
  • Targets the highest-impact queries: expense listing/filtering by date and category, dashboard aggregations by user+type+month, recurring expense dedup, bill listing by active+due date, and reminder processing
  • Includes an idempotent SQL migration script (002_add_indexes.sql) and 16 tests validating index declarations at both SQLAlchemy metadata and database engine levels

Key indexes added

Table Index Columns Rationale
expenses ix_expenses_user_id_spent_at user_id, spent_at Expense listing with date range filter
expenses ix_expenses_user_id_category_id user_id, category_id Category filter on expense list
expenses ix_expenses_user_id_type_spent_at user_id, expense_type, spent_at Dashboard SUM aggregations
expenses ix_expenses_user_id_recurring_spent_at user_id, source_recurring_id, spent_at Recurring expense dedup
categories ix_categories_user_id_name user_id, name (unique) Category uniqueness enforcement
bills ix_bills_user_id_active_due user_id, active, next_due_date Active bills sorted by due date
reminders ix_reminders_user_id_sent_send_at user_id, sent, send_at Unsent reminder processing
reminders ix_reminders_user_id_bill_id_channel_send_at user_id, bill_id, channel, send_at Bill reminder dedup

Closes #128

Test plan

  • 16 new tests in test_db_indexes.py all passing
  • Verify existing test suite still passes (pre-existing Redis connection failures unrelated)
  • Run migration on staging PostgreSQL database
  • Monitor query performance before/after with EXPLAIN ANALYZE on key queries

🤖 Generated with Claude Code

Analyze query patterns across all routes (expenses, dashboard, bills,
reminders, insights) and add targeted single-column and composite indexes
to eliminate full table scans on the most frequent queries:

- expenses: (user_id, spent_at), (user_id, category_id),
  (user_id, expense_type, spent_at), (user_id, source_recurring_id, spent_at)
- categories: (user_id, name) unique composite
- bills: (user_id, active, next_due_date)
- reminders: (user_id, sent, send_at), (user_id, bill_id, channel, send_at)
- recurring_expenses: (user_id, active)
- audit_logs: (user_id, created_at)
- Single-column indexes on all user_id FK columns and timestamp columns

Includes idempotent SQL migration script and 16 tests verifying index
declarations at both the SQLAlchemy metadata and database engine levels.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@mendarb mendarb requested a review from rohitdash08 as a code owner March 20, 2026 14:13
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.

Database indexing optimization for financial queries

1 participant