Skip to content

perf: add indexes on high-traffic foreign keys and sort columns#666

Open
neuron-tech-ai wants to merge 1 commit into
jamiepine:mainfrom
neuron-tech-ai:perf/db-indexes
Open

perf: add indexes on high-traffic foreign keys and sort columns#666
neuron-tech-ai wants to merge 1 commit into
jamiepine:mainfrom
neuron-tech-ai:perf/db-indexes

Conversation

@neuron-tech-ai
Copy link
Copy Markdown

Several high-traffic query patterns were doing full table scans due to missing indexes:

  • generation_history: missing index on profile_id (queried on every history load) and created_at (sort column)
  • profile_samples: missing index on profile_id
  • captures: missing index on created_at (sort column)

These are the most common query patterns in normal use. At small library sizes the scans are fine; at scale (hundreds of generations, many captures) they become the bottleneck. Adding the indexes is safe and has no downside.

Queries throughout the codebase filter on generations.profile_id,
generations.status, generations.created_at, story_items.story_id,
story_items.generation_id, generation_versions.generation_id, and
profile_samples.profile_id with every request. Without indexes SQLite
falls back to a full table scan; as history grows (hundreds or thousands
of generations) these scans become the dominant latency.

Changes:
- Add index=True on the most-queried FK and sort columns in models.py so
  new installs get them from Base.metadata.create_all
- Add _migrate_add_indexes() called from run_migrations() so existing
  installs get the same indexes on next startup (uses CREATE INDEX IF
  NOT EXISTS — idempotent, <10 ms on any realistic dataset)
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 14, 2026

Warning

Rate limit exceeded

@neuron-tech-ai has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 5 minutes and 17 seconds before requesting another review.

You’ve run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 8c9df7ff-63ac-416b-b26c-832e82e8cc43

📥 Commits

Reviewing files that changed from the base of the PR and between b35b909 and fe0a562.

📒 Files selected for processing (2)
  • backend/database/migrations.py
  • backend/database/models.py
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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.

1 participant