From fe9639e659293f683005ea5add5ea7e8535da2c7 Mon Sep 17 00:00:00 2001 From: ytqh Date: Mon, 21 Apr 2025 10:16:56 +0800 Subject: [PATCH] optimize index --- ...21_1011-ad332d76d058_add_index_of_model.py | 33 +++++++++++++++++++ api/models/model.py | 1 + 2 files changed, 34 insertions(+) create mode 100644 api/migrations/versions/2025_04_21_1011-ad332d76d058_add_index_of_model.py diff --git a/api/migrations/versions/2025_04_21_1011-ad332d76d058_add_index_of_model.py b/api/migrations/versions/2025_04_21_1011-ad332d76d058_add_index_of_model.py new file mode 100644 index 0000000000..858b847d21 --- /dev/null +++ b/api/migrations/versions/2025_04_21_1011-ad332d76d058_add_index_of_model.py @@ -0,0 +1,33 @@ +"""add index of model + +Revision ID: ad332d76d058 +Revises: 69f4e59ecafb +Create Date: 2025-04-21 10:11:53.584079 + +""" +from alembic import op +import models as models +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'ad332d76d058' +down_revision = '69f4e59ecafb' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('messages', schema=None) as batch_op: + batch_op.create_index('message_end_user_org_created_idx', ['from_end_user_id', 'organization_id', 'created_at'], unique=False) + + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('messages', schema=None) as batch_op: + batch_op.drop_index('message_end_user_org_created_idx') + + # ### end Alembic commands ### diff --git a/api/models/model.py b/api/models/model.py index 47dd99743e..0d97cf2569 100644 --- a/api/models/model.py +++ b/api/models/model.py @@ -793,6 +793,7 @@ class Message(db.Model): # type: ignore[name-defined] db.Index("message_workflow_run_id_idx", "conversation_id", "workflow_run_id"), db.Index("message_created_at_idx", "created_at"), db.Index("message_organization_id_idx", "organization_id"), + db.Index("message_end_user_org_created_idx", "from_end_user_id", "organization_id", "created_at"), ) id: Mapped[str] = mapped_column(StringUUID, server_default=db.text("uuid_generate_v4()"))