Skip to content

perf: Eliminate N+1 access grant queries in prompts listing#21616

Closed
Classic298 wants to merge 1 commit intoopen-webui:devfrom
Classic298:perf-prompt-list
Closed

perf: Eliminate N+1 access grant queries in prompts listing#21616
Classic298 wants to merge 1 commit intoopen-webui:devfrom
Classic298:perf-prompt-list

Conversation

@Classic298
Copy link
Collaborator

The prompts listing endpoints were calling _get_access_grants() per
prompt (N+1 queries) via _to_prompt_model(). Additionally,
get_prompts_by_user_id() called has_access() per prompt for filtering
(another N+1). Now:

  • get_prompts() batch-fetches all access grants in one query via new
    get_grants_by_resources() method on AccessGrants
  • get_prompts_by_user_id() uses batch get_accessible_resource_ids()
    instead of per-prompt has_access() calls
  • search_prompts() batch-fetches grants instead of per-item
    _to_prompt_model() calls

For 30 prompts this reduces ~31 DB queries to ~3.

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.

The prompts listing endpoints were calling _get_access_grants() per
prompt (N+1 queries) via _to_prompt_model(). Additionally,
get_prompts_by_user_id() called has_access() per prompt for filtering
(another N+1). Now:

- get_prompts() batch-fetches all access grants in one query via new
  get_grants_by_resources() method on AccessGrants
- get_prompts_by_user_id() uses batch get_accessible_resource_ids()
  instead of per-prompt has_access() calls
- search_prompts() batch-fetches grants instead of per-item
  _to_prompt_model() calls

For 30 prompts this reduces ~31 DB queries to ~3.
@pr-validator-bot
Copy link

👋 Welcome and Thank You for Contributing!

We appreciate you taking the time to submit a pull request to Open WebUI!

⚠️ Important: Testing Requirements

We've recently seen an increase in PRs that have significant issues:

  • PRs that don't actually fix the bug they claim to fix
  • PRs that don't implement the feature they describe
  • PRs that break existing functionality
  • PRs that are clearly AI-generated without proper testing being done by the author
  • PRs that simply don't work as intended

These untested PRs consume significant time from maintainers and volunteer contributors who review and test PRs in their free time.
Time that could be spent testing other PRs or improving Open WebUI in other ways.

Before marking your PR as "Ready for Review":

Please explicitly confirm:

  1. ✅ You have personally tested ALL changes in this PR
  2. How you tested it (specific steps you took to verify it works)
  3. Visual evidence where applicable (screenshots or videos showing the feature/fix working) - if applicable to your specific PR

If you're not certain your PR works exactly as intended, please leave it in DRAFT mode until you've thoroughly tested it.

Thank you for helping us maintain quality and respecting the time of our community! 🙏

@pr-validator-bot
Copy link

⚠️ Warning: Possible Non-Atomic / Scope Creep PR Detected

Your PR was subjected to automated review by AI to determine if it could fall under Open WebUI's non-atomicity ruleset or scope creep.

This PR appears to contain multiple unrelated changes that could be split into separate pull requests.

🔍 AI Analysis Summary

Primary Intent: Performance optimization: eliminate N+1 query patterns in prompts retrieval by batch-fetching access grants in a single query instead of making individual has_access() calls

Secondary Changes Detected:

  • New utility method get_grants_by_resources added to AccessGrants model (enabler for the optimization)
📝 Detailed Analysis and Full Report (click to expand)

This PR introduces a new method get_grants_by_resources in access_grants.py that enables batch-fetching of grants. This method is then used to optimize three locations in prompts.py (get_prompts, get_prompts_by_user_id, search_prompts) to eliminate N+1 query patterns.

The 'Pre-factor' test: Could the new get_grants_by_resources utility method have been merged as a separate PR first? YES. The batch-fetch utility is a standalone enhancement that could be merged independently. The prompt optimizations that use this utility could then follow in a subsequent PR.

While the changes are related (same domain), they represent compound intentions: (1) adding a new utility method, and (2) refactoring existing code to use it. Per the strict rules, refactoring/optimization that could have been separate PRs should be split.

Why Atomic PRs With Narrow Scopes Matter

Atomic PRs (single-purpose PRs) are:

  • Easier to review - Reviewers can focus on one thing at a time
  • Easier to test - Each change can be verified independently
  • Easier to revert - If something breaks, we can revert just the problematic change
  • Faster to merge - Smaller, focused PRs get reviewed and merged quicker

What Makes a PR Atomic / Narrow in Scope?

An atomic PR should contain one semantic change:

  • ✅ Just one bug fix (even if it touches multiple files)
  • ✅ Just one feature (even if it requires changes across multiple files)
  • ✅ Just i18n/translation updates
  • ✅ Just documentation updates
  • ✅ Just refactoring of one specific thing
  • ✅ Just one performance improvement

What To Do

This is an automated analysis. If you believe this assessment is incorrect and your PR is actually atomic (all changes serve one unified purpose), please explain in a comment below.

Consider splitting this PR into separate, focused pull requests. Each PR should address one specific thing.

For example, if you have a bug fix and a new feature, submit them as two separate PRs.

@tjbck tjbck closed this Feb 19, 2026
@Classic298 Classic298 deleted the perf-prompt-list branch February 19, 2026 23:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants