fix: optimize message scan sql

This commit is contained in:
hj24 2026-02-11 13:59:13 +08:00
parent d1cdc85a2e
commit f07f844d39
2 changed files with 2 additions and 7 deletions

View File

@ -1038,7 +1038,6 @@ class Message(Base):
Index("message_end_user_idx", "app_id", "from_source", "from_end_user_id"),
Index("message_account_idx", "app_id", "from_source", "from_account_id"),
Index("message_workflow_run_id_idx", "conversation_id", "workflow_run_id"),
Index("message_created_at_idx", "created_at"),
Index("message_app_mode_idx", "app_mode"),
Index("message_created_at_id_idx", "created_at", "id"),
)

View File

@ -6,7 +6,7 @@ import time
from collections.abc import Sequence
from typing import cast
from sqlalchemy import delete, select
from sqlalchemy import delete, select, tuple_
from sqlalchemy.engine import CursorResult
from sqlalchemy.orm import Session
@ -215,13 +215,9 @@ class MessagesCleanService:
msg_stmt = msg_stmt.where(Message.created_at >= self._start_from)
# Apply cursor condition: (created_at, id) > (last_created_at, last_message_id)
# This translates to:
# created_at > last_created_at OR (created_at = last_created_at AND id > last_message_id)
if _cursor:
# Continuing from previous batch
msg_stmt = msg_stmt.where(
(Message.created_at > _cursor[0])
| ((Message.created_at == _cursor[0]) & (Message.id > _cursor[1]))
tuple_(Message.created_at, Message.id) > tuple_(_cursor[0], _cursor[1])
)
raw_messages = list(session.execute(msg_stmt).all())