perf(message): optimize message loading and reduce SQL queries (#13720)

This commit is contained in:
Hundredwz 2025-02-15 12:19:01 +08:00 committed by GitHub
parent 1f63028a83
commit 284707c3a8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 27 deletions

View File

@ -70,7 +70,7 @@ class MessageListApi(Resource):
try:
return MessageService.pagination_by_first_id(
app_model, end_user, args["conversation_id"], args["first_id"], args["limit"]
app_model, end_user, args["conversation_id"], args["first_id"], args["limit"], "desc"
)
except services.errors.conversation.ConversationNotExistsError:
raise NotFound("Conversation Not Exists.")

View File

@ -46,6 +46,8 @@ class MessageService:
app_model=app_model, user=user, conversation_id=conversation_id
)
fetch_limit = limit + 1
if first_id:
first_message = (
db.session.query(Message)
@ -64,7 +66,7 @@ class MessageService:
Message.id != first_message.id,
)
.order_by(Message.created_at.desc())
.limit(limit)
.limit(fetch_limit)
.all()
)
else:
@ -72,25 +74,14 @@ class MessageService:
db.session.query(Message)
.filter(Message.conversation_id == conversation.id)
.order_by(Message.created_at.desc())
.limit(limit)
.limit(fetch_limit)
.all()
)
has_more = False
if len(history_messages) == limit:
current_page_first_message = history_messages[-1]
rest_count = (
db.session.query(Message)
.filter(
Message.conversation_id == conversation.id,
Message.created_at < current_page_first_message.created_at,
Message.id != current_page_first_message.id,
)
.count()
)
if rest_count > 0:
has_more = True
if len(history_messages) > limit:
has_more = True
history_messages = history_messages[:-1]
if order == "asc":
history_messages = list(reversed(history_messages))
@ -112,6 +103,8 @@ class MessageService:
base_query = db.session.query(Message)
fetch_limit = limit + 1
if conversation_id is not None:
conversation = ConversationService.get_conversation(
app_model=app_model, user=user, conversation_id=conversation_id
@ -131,21 +124,16 @@ class MessageService:
history_messages = (
base_query.filter(Message.created_at < last_message.created_at, Message.id != last_message.id)
.order_by(Message.created_at.desc())
.limit(limit)
.limit(fetch_limit)
.all()
)
else:
history_messages = base_query.order_by(Message.created_at.desc()).limit(limit).all()
history_messages = base_query.order_by(Message.created_at.desc()).limit(fetch_limit).all()
has_more = False
if len(history_messages) == limit:
current_page_first_message = history_messages[-1]
rest_count = base_query.filter(
Message.created_at < current_page_first_message.created_at, Message.id != current_page_first_message.id
).count()
if rest_count > 0:
has_more = True
if len(history_messages) > limit:
has_more = True
history_messages = history_messages[:-1]
return InfiniteScrollPagination(data=history_messages, limit=limit, has_more=has_more)