From a97fbc1e1d17100c9d69cb30738e1c001b0700f0 Mon Sep 17 00:00:00 2001 From: Abhimanyu Saharan Date: Thu, 12 Feb 2026 08:13:40 +0000 Subject: [PATCH] perf(db): index board_memory lists and task comments --- ...5f85_add_indexes_for_board_memory_task_.py | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 backend/migrations/versions/99cd6df95f85_add_indexes_for_board_memory_task_.py diff --git a/backend/migrations/versions/99cd6df95f85_add_indexes_for_board_memory_task_.py b/backend/migrations/versions/99cd6df95f85_add_indexes_for_board_memory_task_.py new file mode 100644 index 00000000..3faa87fa --- /dev/null +++ b/backend/migrations/versions/99cd6df95f85_add_indexes_for_board_memory_task_.py @@ -0,0 +1,48 @@ +"""add indexes for board memory + task comments + +Revision ID: 99cd6df95f85 +Revises: f4d2b649e93a +Create Date: 2026-02-12 08:13:19.786621 + +""" +from __future__ import annotations + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '99cd6df95f85' +down_revision = 'f4d2b649e93a' +branch_labels = None +depends_on = None + + +def upgrade() -> None: + # Board memory lists filter on (board_id, is_chat) and order by created_at desc. + op.create_index( + "ix_board_memory_board_id_is_chat_created_at", + "board_memory", + ["board_id", "is_chat", "created_at"], + ) + + # Task comments are stored as ActivityEvent rows with event_type='task.comment'. + # Listing comments uses task_id + created_at ordering, so a partial composite index + # avoids scanning other activity rows. + op.create_index( + "ix_activity_events_task_comment_task_id_created_at", + "activity_events", + ["task_id", "created_at"], + postgresql_where=sa.text("event_type = 'task.comment'"), + ) + + +def downgrade() -> None: + op.drop_index( + "ix_activity_events_task_comment_task_id_created_at", + table_name="activity_events", + ) + op.drop_index( + "ix_board_memory_board_id_is_chat_created_at", + table_name="board_memory", + )