[PR #20999] [CLOSED] perf: resolve N+1 queries in Ollama model access control filtering #48921

Closed
opened 2026-04-30 01:08:22 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

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

Base: devHead: ollama-n+1


📝 Commits (3)

  • 9189fb1 fix: resolve N+1 queries in OLLAMA model access control filtering
  • 8253a1e Update ollama.py
  • cbaa2fe Update ollama.py

📊 Changes

1 file changed (+38 additions, -12 deletions)

View changed files

📝 backend/open_webui/routers/ollama.py (+38 -12)

📄 Description

Summary

Eliminates N+1 database queries in model access control filtering for Ollama routers. Previously, when filtering models for non-admin users, the code called get_model_by_id for each model individually, resulting in N database queries.

Changes

Refactored two locations in ollama.py:

  • get_filtered_models function (used by /api/tags endpoint)
  • Inline filtering in /v1/models endpoint
    Both now use get_models_by_ids to batch-fetch all model configurations in a single query, then filter using a lookup dictionary.

Performance Impact

Before: N+1 queries (1 query per model)
After: 1 query total using SQL IN clause

Code Quality

Used descriptive variable names for readability:

  • model_ids_to_check: the IDs we need to look up
  • db_models: models fetched from database
  • model_configs_by_id: lookup dictionary (model_id -> config)
  • user_owns_model / user_has_read_access: explicit boolean conditions

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/20999 **Author:** [@Classic298](https://github.com/Classic298) **Created:** 1/28/2026 **Status:** ❌ Closed **Base:** `dev` ← **Head:** `ollama-n+1` --- ### 📝 Commits (3) - [`9189fb1`](https://github.com/open-webui/open-webui/commit/9189fb1685d6019ead32a122f0c4f71e568556bd) fix: resolve N+1 queries in OLLAMA model access control filtering - [`8253a1e`](https://github.com/open-webui/open-webui/commit/8253a1eb18a63f9df0d7ecbcd61a7ea412e0c76c) Update ollama.py - [`cbaa2fe`](https://github.com/open-webui/open-webui/commit/cbaa2fe51cc2b763c1d9e137277cc405e98e8b78) Update ollama.py ### 📊 Changes **1 file changed** (+38 additions, -12 deletions) <details> <summary>View changed files</summary> 📝 `backend/open_webui/routers/ollama.py` (+38 -12) </details> ### 📄 Description ## Summary Eliminates N+1 database queries in model access control filtering for Ollama routers. Previously, when filtering models for non-admin users, the code called get_model_by_id for each model individually, resulting in N database queries. ## Changes Refactored two locations in ollama.py: - get_filtered_models function (used by /api/tags endpoint) - Inline filtering in /v1/models endpoint Both now use get_models_by_ids to batch-fetch all model configurations in a single query, then filter using a lookup dictionary. ## Performance Impact Before: N+1 queries (1 query per model) After: 1 query total using SQL IN clause ## Code Quality Used descriptive variable names for readability: - model_ids_to_check: the IDs we need to look up - db_models: models fetched from database - model_configs_by_id: lookup dictionary (model_id -> config) - user_owns_model / user_has_read_access: explicit boolean conditions ### 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-04-30 01:08:22 -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#48921