From b21b90533ec988fada75de2ee347902a7af91fd9 Mon Sep 17 00:00:00 2001
From: Classic298 <27028174+Classic298@users.noreply.github.com>
Date: Mon, 23 Nov 2026 18:56:08 +0100
Subject: [PATCH] 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.
| search | 1,400 chats before | after | 7,500 chats before | after |
| All, empty | 1.2 s | 0.2 s | 2.3 s | 0.3 s |
| All, a common word | 10.8 s | 1.0 s | 12.5 s | 7.6 s |
| All, two words | 18.2 s | 1.4 s | 20.8 s | 14.3 s |
| All, a rare word | 14.5 s | 2.4 s | 15 s | 8.9 s |
| Created by you, a common word | 0.04 s | 0.04 s | 7.0 s | 0.15 s |
| Created by you, 14 words | 1.6 s | 0.19 s | 3.9 s | 1.8 s |
Fixes #34903
---
backend/open_webui/routers/search.py | 19 ++++++++++++-------
1 file changed, 12 insertions(+), 7 deletions(-)
diff --git a/backend/open_webui/routers/search.py b/backend/open_webui/routers/search.py
index 4aa8ce012c..93523d4812 100644
--- a/backend/open_webui/routers/search.py
+++ b/backend/open_webui/routers/search.py
@@ -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)