perf: add index on memory.user_id

Memory queries filter by user_id (get_memories_by_user_id,
delete_memories_by_user_id) but the column had no index, forcing
a full table scan per request. Adds a btree index via Alembic
migration plus index=True on the model so fresh installs include it.
This commit is contained in:
Claude 2026-04-17 07:59:13 +00:00
parent 7cfb260b8a
commit f8b39eed19
No known key found for this signature in database
2 changed files with 27 additions and 1 deletions

View file

@ -0,0 +1,26 @@
"""Add index on memory.user_id
Revision ID: a749ab2aa953
Revises: c1d2e3f4a5b6
Create Date: 2026-04-17 00:00:00.000000
"""
from alembic import op
import sqlalchemy as sa
revision = 'a749ab2aa953'
down_revision = 'c1d2e3f4a5b6'
branch_labels = None
depends_on = None
def upgrade():
# Memory.user_id is filtered on every per-user query
# (get_memories_by_user_id, delete_memories_by_user_id),
# so a btree index prevents full-table scans as the table grows.
op.create_index('memory_user_id_idx', 'memory', ['user_id'])
def downgrade():
op.drop_index('memory_user_id_idx', table_name='memory')

View file

@ -19,7 +19,7 @@ class Memory(Base):
__tablename__ = 'memory'
id = Column(String, primary_key=True, unique=True)
user_id = Column(String)
user_id = Column(String, index=True)
content = Column(Text)
updated_at = Column(BigInteger)
created_at = Column(BigInteger)