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>
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/20427
Author: @Classic298
Created: 1/6/2026
Status: ✅ Merged
Merged: 1/6/2026
Merged by: @tjbck
Base:
dev← Head:n+1📝 Commits (1)
ca508b0fix: 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/usersendpoint where groups were being fetched for each user individually.Problem
The
GET /api/v1/usersendpoint calledGroups.get_groups_by_member_id()for each user, resulting in:Solution
Added a new
Groups.get_groups_by_member_ids()method that fetches groups for multiple users in a single query using SQLINclause andJOIN.Result
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.