Skip to content

feat: database indexing optimization for financial queries (#128)#395

Open
sinatragianpaolo-oc wants to merge 3 commits intorohitdash08:mainfrom
sinatragianpaolo-oc:feat/db-indexing
Open

feat: database indexing optimization for financial queries (#128)#395
sinatragianpaolo-oc wants to merge 3 commits intorohitdash08:mainfrom
sinatragianpaolo-oc:feat/db-indexing

Conversation

@sinatragianpaolo-oc
Copy link

@sinatragianpaolo-oc sinatragianpaolo-oc commented Mar 14, 2026

Adds database indexing optimization for financial queries to fix #128.

007_db_indexing.sql creates targeted indexes for the hottest query patterns (expenses date-range, monthly aggregation, bills/reminders dispatch, categories). flask index-report CLI command shows current indexes vs. recommended list.

GET /admin/db/indexes (JWT + ADMIN role) — HTTP JSON equivalent of the CLI report, returning existing, required, missing, extra, and healthy fields.


Review fixes

  1. REQUIRED_INDEXES / migration mismatch fixed — four indexes (idx_expenses_user_spent_at, idx_bills_user_due, idx_reminders_due, idx_recurring_expenses_user_start) were listed in REQUIRED_INDEXES but absent from the SQL. Added the missing CREATE INDEX IF NOT EXISTS statements.
  2. DATE_TRUNC PostgreSQL-only caveat documented — comment added above idx_expenses_user_month.
  3. GET /admin/db/indexes endpoint — returns the same data as the CLI in JSON; admin-only (403 for regular users).

Tests: test_required_indexes_match_migration (static regression guard), test_date_trunc_index_is_postgres_only, TestAdminDbIndexesEndpoint (auth, role, structure, required-list match).

Closes #128

…08#128)

- Migration 007_db_indexing.sql: 13 new indexes covering all hot paths
  - expenses: category filter, type split, monthly aggregation, amount range
  - bills: active-only partial index, autopay partial index
  - reminders: pending dispatch partial index, bill FK
  - recurring_expenses: active partial index
  - categories: user+name composite
  - audit_logs: user+date, action+date
  - user_subscriptions: active partial index
- db/index_report.py: flask index-report CLI + check_missing_indexes()
- 9 tests: index list validation + query pattern integration tests

Closes rohitdash08#128
sinatragianpaolo-oc added 2 commits March 16, 2026 19:09
Fix 1 — REQUIRED_INDEXES / migration mismatch
  Four indexes listed in REQUIRED_INDEXES were not created by
  007_db_indexing.sql, causing 'flask index-report' to always report them
  as missing even after applying the migration.

  Added the four missing CREATE INDEX statements to 007_db_indexing.sql:
    - idx_expenses_user_spent_at  : basic user+date DESC for simple range queries
    - idx_bills_user_due          : full (non-partial) user+next_due_date
    - idx_reminders_due           : full user+send_at for per-user listing
    - idx_recurring_expenses_user_start : full user+start_date for history views

  Each complements its existing partial-index sibling already in the file:
  the full index covers queries that cannot use the partial WHERE clause.

Fix 2 — DATE_TRUNC PostgreSQL-only caveat documented
  Added an explicit comment block directly above the
  idx_expenses_user_month CREATE INDEX statement warning that DATE_TRUNC
  is a PostgreSQL-specific function and that this statement will fail on
  SQLite and MySQL.

Tests updated:
  - test_critical_indexes_documented: now asserts every REQUIRED_INDEXES
    entry including all four previously missing ones
  - test_required_indexes_match_migration (new): static regression guard
    that reads 007_db_indexing.sql and asserts every name in
    REQUIRED_INDEXES has a corresponding CREATE INDEX statement — prevents
    the lists from drifting again
  - test_date_trunc_index_is_postgres_only (new): reads the migration file
    and asserts that a 'postgresql'/'postgres' warning comment appears in
    the context preceding idx_expenses_user_month
Adds a JSON HTTP equivalent of the 'flask index-report' CLI command.

GET /admin/db/indexes (JWT + ADMIN role required) returns:
  existing  — all current DB indexes
  required  — REQUIRED_INDEXES constant
  missing   — required indexes not yet present
  extra     — indexes present but not in required list
  healthy   — true when missing is empty

On non-PostgreSQL engines (SQLite in tests) existing is empty and all
required indexes appear as missing — same behaviour as the CLI.

Tests added (TestAdminDbIndexesEndpoint):
  - 401 without auth
  - 403 for non-admin users
  - 200 + correct structure for admin
  - required list matches REQUIRED_INDEXES constant
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