[PR #21017] [CLOSED] perf: eliminate N+1 tag operations in chats.py #80762

Closed
opened 2026-05-13 14:59:04 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/open-webui/open-webui/pull/21017
Author: @Classic298
Created: 1/28/2026
Status: Closed

Base: devHead: chat-tags-n+1


📝 Commits (1)

  • 22532bc perf: 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

  • 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), 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>
GiteaMirror added the pull-request label 2026-05-13 14:59:04 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/open-webui#80762