[PR #20427] [MERGED] fix: resolve N+1 query pattern in users endpoint #112933

Closed
opened 2026-05-18 13:10:10 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/open-webui/open-webui/pull/20427
Author: @Classic298
Created: 1/6/2026
Status: Merged
Merged: 1/6/2026
Merged by: @tjbck

Base: devHead: n+1


📝 Commits (1)

  • ca508b0 fix: resolve N+1 query pattern in users endpoint

📊 Changes

2 files changed (+26 additions, -4 deletions)

View changed files

📝 backend/open_webui/models/groups.py (+21 -0)
📝 backend/open_webui/routers/users.py (+5 -4)

📄 Description

Summary

Fixed N+1 query pattern in the /api/v1/users endpoint where groups were being fetched for each user individually.

Problem

The GET /api/v1/users endpoint called Groups.get_groups_by_member_id() for each user, resulting in:

  • 1 query for users
  • N queries for groups (one per user)

Solution

Added a new Groups.get_groups_by_member_ids() method that fetches groups for multiple users in a single query using SQL IN clause and JOIN.

Result

  • Before: 1 + N queries
  • After: 2 queries total (1 for users, 1 for all groups)

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/20427 **Author:** [@Classic298](https://github.com/Classic298) **Created:** 1/6/2026 **Status:** ✅ Merged **Merged:** 1/6/2026 **Merged by:** [@tjbck](https://github.com/tjbck) **Base:** `dev` ← **Head:** `n+1` --- ### 📝 Commits (1) - [`ca508b0`](https://github.com/open-webui/open-webui/commit/ca508b06bd7d6dafa7fc9b0fc6321de31dff0b50) fix: resolve N+1 query pattern in users endpoint ### 📊 Changes **2 files changed** (+26 additions, -4 deletions) <details> <summary>View changed files</summary> 📝 `backend/open_webui/models/groups.py` (+21 -0) 📝 `backend/open_webui/routers/users.py` (+5 -4) </details> ### 📄 Description ## Summary Fixed N+1 query pattern in the `/api/v1/users` endpoint where groups were being fetched for each user individually. ### Problem The `GET /api/v1/users` endpoint called `Groups.get_groups_by_member_id()` for each user, resulting in: - 1 query for users - N queries for groups (one per user) ### Solution Added a new `Groups.get_groups_by_member_ids()` method that fetches groups for multiple users in a single query using SQL `IN` clause and `JOIN`. ### Result - Before: 1 + N queries - After: 2 queries total (1 for users, 1 for all groups) ### 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-18 13:10:10 -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#112933