mirror of
https://github.com/open-webui/open-webui.git
synced 2026-11-16 09:41:23 +00:00
perf: channel message pages load much faster when many messages are replies (#33691)
Opening a channel looked up every replied-to message one by one, so a page of 50 mostly-reply messages ran well over a hundred database queries. The replied-to messages and their authors are now fetched together, and the response is unchanged. Measured on a local SQLite database with a 50-message page where 46 messages are replies: | | before | after | |---|---|---| | queries | 147 | 3 | | time (median) | 46 ms | 1.9 ms |
This commit is contained in:
parent
332f6a2586
commit
9f748cb5c9
1 changed files with 25 additions and 7 deletions
|
|
@ -412,14 +412,11 @@ class MessageTable:
|
|||
)
|
||||
all_messages = result.scalars().all()
|
||||
|
||||
reply_to_messages = await self._get_reply_to_messages(all_messages, db)
|
||||
|
||||
messages = []
|
||||
for message in all_messages:
|
||||
reply_to_message = (
|
||||
await self.get_message_by_id(message.reply_to_id, include_thread_replies=False, db=db)
|
||||
if message.reply_to_id
|
||||
else None
|
||||
)
|
||||
|
||||
reply_to_message = reply_to_messages.get(message.reply_to_id)
|
||||
user_info = await self._resolve_user_info(message, db)
|
||||
|
||||
messages.append(
|
||||
|
|
@ -427,12 +424,33 @@ class MessageTable:
|
|||
{
|
||||
**MessageModel.model_validate(message).model_dump(),
|
||||
'user': user_info,
|
||||
'reply_to_message': (reply_to_message.model_dump() if reply_to_message else None),
|
||||
'reply_to_message': reply_to_message,
|
||||
}
|
||||
)
|
||||
)
|
||||
return messages
|
||||
|
||||
async def _get_reply_to_messages(self, messages: list[Message], db: AsyncSession) -> dict[str, dict]:
|
||||
"""Fetch every reply target of the given messages in a fixed number of queries, keyed by message id."""
|
||||
reply_to_ids = {message.reply_to_id for message in messages if message.reply_to_id}
|
||||
if not reply_to_ids:
|
||||
return {}
|
||||
|
||||
result = await db.execute(select(Message).filter(Message.id.in_(reply_to_ids)))
|
||||
targets = {message.id: message for message in result.scalars().all()}
|
||||
|
||||
user_infos = {target_id: await self._resolve_user_info(target, db) for target_id, target in targets.items()}
|
||||
author_ids = [target.user_id for target_id, target in targets.items() if user_infos[target_id] is None]
|
||||
authors = {user.id: user for user in await Users.get_users_by_user_ids(author_ids, db=db)}
|
||||
|
||||
reply_to_messages = {}
|
||||
for target_id, target in targets.items():
|
||||
user_info = user_infos[target_id]
|
||||
if user_info is None and target.user_id in authors:
|
||||
user_info = authors[target.user_id].model_dump()
|
||||
reply_to_messages[target_id] = {**MessageModel.model_validate(target).model_dump(), 'user': user_info}
|
||||
return reply_to_messages
|
||||
|
||||
async def get_messages_by_parent_id(
|
||||
self,
|
||||
channel_id: str,
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue