fix: add partial indexes on conversations for app_id with created_at/updated_at to resolve slow chat-conversations query

This commit is contained in:
Novice
2026-03-01 10:55:10 +08:00
parent ee1aee01a3
commit 80360da8b6
2 changed files with 6 additions and 6 deletions

View File

@@ -21,17 +21,17 @@ def upgrade():
'conversation_app_created_at_idx',
['app_id', sa.literal_column('created_at DESC')],
unique=False,
postgresql_where=sa.text('is_deleted = false'),
postgresql_where=sa.text('is_deleted IS false'),
)
batch_op.create_index(
'conversation_app_updated_at_idx',
['app_id', sa.literal_column('updated_at DESC')],
unique=False,
postgresql_where=sa.text('is_deleted = false'),
postgresql_where=sa.text('is_deleted IS false'),
)
def downgrade():
with op.batch_alter_table('conversations', schema=None) as batch_op:
batch_op.drop_index('conversation_app_updated_at_idx', postgresql_where=sa.text('is_deleted = false'))
batch_op.drop_index('conversation_app_created_at_idx', postgresql_where=sa.text('is_deleted = false'))
batch_op.drop_index('conversation_app_updated_at_idx', postgresql_where=sa.text('is_deleted IS false'))
batch_op.drop_index('conversation_app_created_at_idx', postgresql_where=sa.text('is_deleted IS false'))

View File

@@ -715,13 +715,13 @@ class Conversation(Base):
"conversation_app_created_at_idx",
"app_id",
sa.text("created_at DESC"),
postgresql_where=sa.text("is_deleted = false"),
postgresql_where=sa.text("is_deleted IS false"),
),
sa.Index(
"conversation_app_updated_at_idx",
"app_id",
sa.text("updated_at DESC"),
postgresql_where=sa.text("is_deleted = false"),
postgresql_where=sa.text("is_deleted IS false"),
),
)