Browse Source

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

Hundredwz 2 months ago
parent
commit
284707c3a8
2 changed files with 15 additions and 27 deletions
  1. 1 1
      api/controllers/service_api/app/message.py
  2. 14 26
      api/services/message_service.py

+ 1 - 1
api/controllers/service_api/app/message.py

@@ -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.")

+ 14 - 26
api/services/message_service.py

@@ -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)