Skip to content

perf(db): add composite indexes for memory recency and per-user practice results#83

Open
tongshu2023 wants to merge 1 commit into
zijinz456:mainfrom
tongshu2023:claude/issue-31-db-indexes
Open

perf(db): add composite indexes for memory recency and per-user practice results#83
tongshu2023 wants to merge 1 commit into
zijinz456:mainfrom
tongshu2023:claude/issue-31-db-indexes

Conversation

@tongshu2023

Copy link
Copy Markdown

Summary

Closes #31.

Changes

  • conversation_memories gains the composite index (user_id, course_id, created_at) — recency-ordered memory lookups (WHERE user_id/course_id ... ORDER BY created_at) stop full-scanning past 10K records. The existing (user_id, course_id) index is kept for planner choice on non-ordered lookups.
  • practice_results gains (user_id, problem_id) for per-user answer history (mastery and wrong-answer queries). The table previously had no secondary indexes at all.
  • Alembic migration 20260610_0025 in the house style: inspector-guarded create/drop, early return on SQLite (env.py skips Alembic for sqlite URLs; the local schema picks the indexes up from the models via create_all). alembic heads verified single-head.
  • tests/test_model_indexes.py pins both index definitions in the SQLAlchemy metadata so they can't silently disappear.

Heads-up for the merge queue

PR #79 (FK ondelete rules, also branched from current main) adds migration 20260610_0024 with the same down_revision. Whichever PR merges second needs a one-line down_revision bump to keep a single head — happy to rebase this one if #79 lands first.

Tests

test_model_indexes.py + test_loom.py: 39 passed locally; migration compiles and alembic heads shows a single head.

Acceptance criteria

  • Indexes added to SQLAlchemy models
  • Alembic migration created and tested
  • Existing tests pass

🤖 Generated with Claude Code

…ice results (zijinz456#31)

- conversation_memories gains (user_id, course_id, created_at) so
  recency-ordered memory lookups stop full-scanning past 10K records;
  the existing (user_id, course_id) index stays for planner choice on
  non-ordered lookups.
- practice_results gains (user_id, problem_id) for per-user answer
  history (mastery and wrong-answer queries).
- Alembic migration 20260610_0025 (house style: inspector-guarded
  create/drop, sqlite early-return since env.py skips Alembic there and
  create_all picks the indexes up from the models). Single head
  verified via alembic heads.
- tests/test_model_indexes.py pins both index definitions in metadata.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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 missing database indexes for query performance

1 participant