Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

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

Hundredwz284707c3a821
diff --git a/api/controllers/service_api/app/message.py b/api/controllers/service_api/app/message.py
index 1869cf67c2e..d5a5afd6a6a 100644
--- a/api/controllers/service_api/app/message.py
+++ b/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.")
diff --git a/api/services/message_service.py b/api/services/message_service.py
index c17122ef647..480d0386233 100644
--- a/api/services/message_service.py
+++ b/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)