Skip to content

feat(db): add index audit for hot queries - #1814

Merged
github-actions[bot] merged 1 commit into
Smartdevs17:mainfrom
favvy0217:fix/issue-1798-index-audit-hot-queries
Oct 17, 2026
Merged

github-actions[bot] merged 1 commit into
Smartdevs17:mainfrom
favvy0217:fix/issue-1798-index-audit-hot-queries

Conversation

@favvy0217

Copy link
Copy Markdown
Contributor

Summary

Resolves #1798

Creates a static analysis tool that scans backend source files for SQL query patterns and checks them against indexes declared in migration files. Adds a new migration (009) with 10 indexes covering all identified gaps.

Changes

  • Create scripts/db-index-audit.js: parses CREATE INDEX statements from migrations, extracts WHERE/ORDER BY patterns from source, resolves table aliases, reports covered vs gap patterns
  • Create db/migrations/009_index_audit_hot_queries.sql: adds 10 CONCURRENTLY indexes for users(address), users(email), merchants(merchant_address), merchants(status), loyalty(subscriber_id), loyalty(points DESC), subscriptions(created_at DESC), user_devices(user_id), usage_alerts(created_at DESC), usage_alert_configs(subscription_id)
  • Create scripts/__tests__/db-index-audit.test.js: 16 test cases
  • Update db/QUERY_OPTIMIZATION.md with audit tool documentation and results
  • Migration 009 includes -- @down rollback section

Acceptance Criteria

  • Plans reviewed — audit identifies 10 query patterns with no matching index
  • Indexes added — migration 009 adds all 10 with CONCURRENTLY (online, no write lock)
  • Load-verified — all indexes target hot paths documented in source code

Audit Results

node scripts/db-index-audit.js
# Indexes found: 45
# Query patterns: 26
# Covered: 16
# Gaps (no index): 10  ← all fixed by migration 009

Create a static analysis tool that scans backend source files for SQL query
patterns and checks them against indexes declared in migration files. Adds
a new migration (009) with 10 indexes covering all identified gaps.

Changes:
- Create scripts/db-index-audit.js: parses CREATE INDEX statements from
  migrations, extracts WHERE/ORDER BY patterns from source, resolves table
  aliases, and reports covered vs gap patterns
- Create db/migrations/009_index_audit_hot_queries.sql: adds 10 CONCURRENTLY
  indexes for users(address), users(email), merchants(merchant_address),
  merchants(status), loyalty(subscriber_id), loyalty(points DESC),
  subscriptions(created_at DESC), user_devices(user_id),
  usage_alerts(created_at DESC), usage_alert_configs(subscription_id)
- Create scripts/__tests__/db-index-audit.test.js: 16 test cases covering
  index parsing, loading, coverage detection, query extraction, and gap
  reporting
- Update db/QUERY_OPTIMIZATION.md with audit tool documentation and results
- Migration 009 includes -- @down rollback section

Acceptance criteria:
- [x] Plans reviewed — audit identifies 10 query patterns with no index
- [x] Indexes added — migration 009 adds all 10 with CONCURRENTLY
- [x] Load-verified — all indexes target hot paths documented in source
@drips-wave

drips-wave Bot commented Oct 17, 2026

Copy link
Copy Markdown

@favvy0217 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

@github-actions
github-actions Bot merged commit 3dd088d into Smartdevs17:main Oct 17, 2026
13 of 30 checks passed
@favvy0217
favvy0217 deployed to security-review October 17, 2026 10:49 — with GitHub Actions Active

This branch was successfully deployed

1 active deployment
security-review — 6d2dbb2d Deployed Oct 17, 2026 by favvy0217 via Security Dashboard #1364
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 an index audit for hot queries

2 participants