mirror of
https://git.mirrors.martin98.com/https://github.com/langgenius/dify.git
synced 2025-08-12 23:39:01 +08:00
perf(message): optimize message loading and reduce SQL queries (#13720)
This commit is contained in:
parent
1f63028a83
commit
284707c3a8
@ -70,7 +70,7 @@ class MessageListApi(Resource):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
return MessageService.pagination_by_first_id(
|
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:
|
except services.errors.conversation.ConversationNotExistsError:
|
||||||
raise NotFound("Conversation Not Exists.")
|
raise NotFound("Conversation Not Exists.")
|
||||||
|
@ -46,6 +46,8 @@ class MessageService:
|
|||||||
app_model=app_model, user=user, conversation_id=conversation_id
|
app_model=app_model, user=user, conversation_id=conversation_id
|
||||||
)
|
)
|
||||||
|
|
||||||
|
fetch_limit = limit + 1
|
||||||
|
|
||||||
if first_id:
|
if first_id:
|
||||||
first_message = (
|
first_message = (
|
||||||
db.session.query(Message)
|
db.session.query(Message)
|
||||||
@ -64,7 +66,7 @@ class MessageService:
|
|||||||
Message.id != first_message.id,
|
Message.id != first_message.id,
|
||||||
)
|
)
|
||||||
.order_by(Message.created_at.desc())
|
.order_by(Message.created_at.desc())
|
||||||
.limit(limit)
|
.limit(fetch_limit)
|
||||||
.all()
|
.all()
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
@ -72,25 +74,14 @@ class MessageService:
|
|||||||
db.session.query(Message)
|
db.session.query(Message)
|
||||||
.filter(Message.conversation_id == conversation.id)
|
.filter(Message.conversation_id == conversation.id)
|
||||||
.order_by(Message.created_at.desc())
|
.order_by(Message.created_at.desc())
|
||||||
.limit(limit)
|
.limit(fetch_limit)
|
||||||
.all()
|
.all()
|
||||||
)
|
)
|
||||||
|
|
||||||
has_more = False
|
has_more = False
|
||||||
if len(history_messages) == limit:
|
if len(history_messages) > limit:
|
||||||
current_page_first_message = history_messages[-1]
|
has_more = True
|
||||||
rest_count = (
|
history_messages = history_messages[:-1]
|
||||||
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 order == "asc":
|
if order == "asc":
|
||||||
history_messages = list(reversed(history_messages))
|
history_messages = list(reversed(history_messages))
|
||||||
@ -112,6 +103,8 @@ class MessageService:
|
|||||||
|
|
||||||
base_query = db.session.query(Message)
|
base_query = db.session.query(Message)
|
||||||
|
|
||||||
|
fetch_limit = limit + 1
|
||||||
|
|
||||||
if conversation_id is not None:
|
if conversation_id is not None:
|
||||||
conversation = ConversationService.get_conversation(
|
conversation = ConversationService.get_conversation(
|
||||||
app_model=app_model, user=user, conversation_id=conversation_id
|
app_model=app_model, user=user, conversation_id=conversation_id
|
||||||
@ -131,21 +124,16 @@ class MessageService:
|
|||||||
history_messages = (
|
history_messages = (
|
||||||
base_query.filter(Message.created_at < last_message.created_at, Message.id != last_message.id)
|
base_query.filter(Message.created_at < last_message.created_at, Message.id != last_message.id)
|
||||||
.order_by(Message.created_at.desc())
|
.order_by(Message.created_at.desc())
|
||||||
.limit(limit)
|
.limit(fetch_limit)
|
||||||
.all()
|
.all()
|
||||||
)
|
)
|
||||||
else:
|
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
|
has_more = False
|
||||||
if len(history_messages) == limit:
|
if len(history_messages) > limit:
|
||||||
current_page_first_message = history_messages[-1]
|
has_more = True
|
||||||
rest_count = base_query.filter(
|
history_messages = history_messages[:-1]
|
||||||
Message.created_at < current_page_first_message.created_at, Message.id != current_page_first_message.id
|
|
||||||
).count()
|
|
||||||
|
|
||||||
if rest_count > 0:
|
|
||||||
has_more = True
|
|
||||||
|
|
||||||
return InfiniteScrollPagination(data=history_messages, limit=limit, has_more=has_more)
|
return InfiniteScrollPagination(data=history_messages, limit=limit, has_more=has_more)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user