Added delete_orphaned_tags_for_chat helper that handles orphan cleanup in fewer queries
routers/chats.py
delete_chat_by_id: Replaced N+1 tag cleanup loop with delete_orphaned_tags_for_chat
toggle_chat_archive_by_id: Replaced N+1 loops for both archiving (orphan cleanup) and unarchiving (tag restoration) with batch methods
delete_all_tags_by_id: Replaced N+1 cleanup loop with batch helper
Performance Impact
Before: 2N queries per operation (1 count + 1 delete per tag)
After: N count queries + batch deletes (still iterates for counts due to JSON complexity, but consolidated tag deletes)
For a chat with 5 tags being deleted:
Before: 10 queries minimum
After: 5 count queries + 1 batch operation
Contributor License Agreement
By submitting this pull request, I confirm that I have read and fully agree to the Contributor License Agreement (CLA), and I am providing my contributions under its terms.
Note
Deleting the CLA section will lead to immediate closure of your PR and it will not be merged in.
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.
## 📋 Pull Request Information
**Original PR:** https://github.com/open-webui/open-webui/pull/21017
**Author:** [@Classic298](https://github.com/Classic298)
**Created:** 1/28/2026
**Status:** ❌ Closed
**Base:** `dev` ← **Head:** `chat-tags-n+1`
---
### 📝 Commits (1)
- [`22532bc`](https://github.com/open-webui/open-webui/commit/22532bcc0370d50f7ae15907145aa66e0a810ab9) perf: eliminate N+1 tag operations in chats.py
### 📊 Changes
**2 files changed** (+96 additions, -25 deletions)
<details>
<summary>View changed files</summary>
📝 `backend/open_webui/models/chats.py` (+76 -2)
📝 `backend/open_webui/routers/chats.py` (+20 -23)
</details>
### 📄 Description
## Summary
Batch-optimized tag operations when deleting, archiving, and managing chat tags.
## Changes
### models/chats.py
- Fixed get_chat_tags_by_id_and_user_id to use batch get_tags_by_ids_and_user_id instead of N individual queries
- Added count_chats_by_tag_names_and_user_id batch method
- Added delete_orphaned_tags_for_chat helper that handles orphan cleanup in fewer queries
### routers/chats.py
- delete_chat_by_id: Replaced N+1 tag cleanup loop with delete_orphaned_tags_for_chat
- toggle_chat_archive_by_id: Replaced N+1 loops for both archiving (orphan cleanup) and unarchiving (tag restoration) with batch methods
- delete_all_tags_by_id: Replaced N+1 cleanup loop with batch helper
## Performance Impact
Before: 2N queries per operation (1 count + 1 delete per tag)
After: N count queries + batch deletes (still iterates for counts due to JSON complexity, but consolidated tag deletes)
For a chat with 5 tags being deleted:
- Before: 10 queries minimum
- After: 5 count queries + 1 batch operation
### Contributor License Agreement
By submitting this pull request, I confirm that I have read and fully agree to the [Contributor License Agreement (CLA)](https://github.com/open-webui/open-webui/blob/main/CONTRIBUTOR_LICENSE_AGREEMENT), and I am providing my contributions under its terms.
> [!NOTE]
> Deleting the CLA section will lead to immediate closure of your PR and it will not be merged in.
---
<sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
📋 Pull Request Information
Original PR: https://github.com/open-webui/open-webui/pull/21017
Author: @Classic298
Created: 1/28/2026
Status: ❌ Closed
Base:
dev← Head:chat-tags-n+1📝 Commits (1)
22532bcperf: eliminate N+1 tag operations in chats.py📊 Changes
2 files changed (+96 additions, -25 deletions)
View changed files
📝
backend/open_webui/models/chats.py(+76 -2)📝
backend/open_webui/routers/chats.py(+20 -23)📄 Description
Summary
Batch-optimized tag operations when deleting, archiving, and managing chat tags.
Changes
models/chats.py
routers/chats.py
Performance Impact
Before: 2N queries per operation (1 count + 1 delete per tag)
After: N count queries + batch deletes (still iterates for counts due to JSON complexity, but consolidated tag deletes)
For a chat with 5 tags being deleted:
Contributor License Agreement
By submitting this pull request, I confirm that I have read and fully agree to the Contributor License Agreement (CLA), and I am providing my contributions under its terms.
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.