fix: chat search with View set to All takes minutes when many chats are shared (#34911)

Searching chats in the search dialog with View set to All went through every shared chat on the instance before checking whether the searcher may open it. On PostgreSQL it also read through the messages of all users' chats, only to drop the ones the searcher cannot open, so one search could run for minutes while holding database connections. Now the folders and shared chats the searcher can open are looked up first and only those are searched, which also speeds up Created by you. The results are the same as before, and a user who owns nearly every chat keeps today's speed.

Measured on PostgreSQL with 61,000 chats (640,000 messages) and 1,000 shared chats (410 MB), for one user who can open 1,400 chats and 300 shared chats and one who can open 7,500 chats and 400 shared chats.

<table>
<tr><th>search</th><th>1,400 chats before</th><th>after</th><th>7,500 chats before</th><th>after</th></tr>
<tr><td>All, empty</td><td>1.2 s</td><td>0.2 s</td><td>2.3 s</td><td>0.3 s</td></tr>
<tr><td>All, a common word</td><td>10.8 s</td><td>1.0 s</td><td>12.5 s</td><td>7.6 s</td></tr>
<tr><td>All, two words</td><td>18.2 s</td><td>1.4 s</td><td>20.8 s</td><td>14.3 s</td></tr>
<tr><td>All, a rare word</td><td>14.5 s</td><td>2.4 s</td><td>15 s</td><td>8.9 s</td></tr>
<tr><td>Created by you, a common word</td><td>0.04 s</td><td>0.04 s</td><td>7.0 s</td><td>0.15 s</td></tr>
<tr><td>Created by you, 14 words</td><td>1.6 s</td><td>0.19 s</td><td>3.9 s</td><td>1.8 s</td></tr>
</table>

Fixes #34903
This commit is contained in:
Classic298 authored and GitHub committed 2026-11-23 21:56:08 +04:00
1 parent 6299625426
commit b21b90533e
1 file changed
+12 -7
+12 -7
View File
@@ -278,12 +278,17 @@ async def search_workspace( # noqa: C901 — keep permission scoping and source
sources = {}
if 'chat' in enabled and kind in ('all', 'chat') and not scope.startswith('channel:'):
continue_shared = select(SharedChat.chat_id).where(
SharedChat.chat['share_mode'].as_string() == 'continue', granted('shared_chat', SharedChat.chat_id)
)
live_access = or_(
Chat.user_id == user.id, Chat.folder_id.in_(select(folders.c.id)), Chat.id.in_(continue_shared)
)
shared_chats = []
if not scope and sharing != 'mine' and not chat_filtered:
shared_chats = (
await db.execute(
select(
SharedChat.id, SharedChat.chat_id, SharedChat.chat['share_mode'].as_string().label('share_mode')
).where(granted('shared_chat', SharedChat.chat_id))
)
).all()
continue_shared = [shared_chat.chat_id for shared_chat in shared_chats if shared_chat.share_mode == 'continue']
live_access = or_(Chat.user_id == user.id, Chat.folder_id.in_(folder_map), Chat.id.in_(continue_shared))
live_ids = select(Chat.id).where(live_access)
rank = (
title_rank(Chat.title, query)
@@ -312,7 +317,7 @@ async def search_workspace( # noqa: C901 — keep permission scoping and source
sources['chat'] = (Chat, apply_match(stmt, Chat), rank, Chat.updated_at)
if not scope and sharing != 'mine' and not chat_filtered:
stmt = select(SharedChat).where(
granted('shared_chat', SharedChat.chat_id), ~SharedChat.chat_id.in_(live_ids)
SharedChat.id.in_([shared_chat.id for shared_chat in shared_chats]), ~SharedChat.chat_id.in_(live_ids)
)
if sharing == 'shared':
stmt = stmt.where(SharedChat.user_id != user.id)