From 024181dd2dfef0874cd510fb2295b3155ed85c61 Mon Sep 17 00:00:00 2001
From: Classic298 <27028174+Classic298@users.noreply.github.com>
Date: Sat, 5 Dec 2026 05:04:01 +0100
Subject: [PATCH] perf: knowledge base sync with many folders no longer times
out (#35630)
Before syncing, the sync client asks the server which files and folders changed. To answer, the server worked out each folder's full path by searching the whole folder list again for every parent folder, so the time grew with the square of the folder count. With tens of thousands of folders that took many minutes, blocked the server meanwhile and made the sync time out. The server now looks up each parent directly, so the folder step takes a fraction of a second.
| Folders in the knowledge base (about 5 levels deep) | Before | After |
| 5,000 | 3.1 s | 5 ms |
| 10,000 | 19.8 s | 10 ms |
| 20,000 | 219 s | 43 ms |
| 64,000 | stopped after 25 min | 151 ms |
---
backend/open_webui/routers/knowledge.py | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/backend/open_webui/routers/knowledge.py b/backend/open_webui/routers/knowledge.py
index 7b82c6ca91..5633e73d18 100644
--- a/backend/open_webui/routers/knowledge.py
+++ b/backend/open_webui/routers/knowledge.py
@@ -1976,13 +1976,14 @@ async def sync_knowledge_diff(
existing_directories = await Knowledges.get_all_directories(id, db=db)
# Build directory path lookups
+ directory_by_id = {directory.id: directory for directory in existing_directories}
directory_path_by_id: dict[str, str] = {}
directory_id_by_path: dict[str, str] = {}
for directory in existing_directories:
segments = [directory.name]
parent_id = directory.parent_id
while parent_id:
- parent = next((d for d in existing_directories if d.id == parent_id), None)
+ parent = directory_by_id.get(parent_id)
if not parent:
break
segments.insert(0, parent.name)