Fixes #39336 - Batch taxonomy subtree lookups to avoid N+1 queries#10990
Open
adamruzicka wants to merge 4 commits into
Open
Fixes #39336 - Batch taxonomy subtree lookups to avoid N+1 queries#10990adamruzicka wants to merge 4 commits into
adamruzicka wants to merge 4 commits into
Conversation
When a user belongs to many organizations, authorization performed 1+N SQL queries per taxonomy type to resolve subtree IDs (one subtree_ids pluck per organization). Replace the per-taxonomy loop with a single batched query using OR conditions on the ancestry column. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add ArgumentError guard and test to prevent silent data loss when mixed Organization/Location objects are passed to batch_subtree_ids. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Avoid loading all taxonomy records into memory when finding taxonomies that ignore 'user'. Use a LIKE pre-filter in SQL to narrow the set, then verify with the exact ignore? check. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Contributor
Author
|
With the ignored taxonomies commit: |
Contributor
Author
|
The single-query pluck(:id) returns IDs in arbitrary database order, causing intermittent test failures in filter_test.rb where expected strings embed literal ID sequences. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Heavily inspired by https://community.theforeman.org/t/bug-orgs-and-locs-job-templates-are-broken-in-3-18-probably-3-17-as-well/46388/15
When a user belongs to many organizations, authorization performed 1+N SQL queries per taxonomy type to resolve subtree IDs (one subtree_ids pluck per organization). This patch replaces the per-taxonomy loop with a single batched query using OR conditions on the ancestry column.
Alternatives would be using recursive CTEs to let postgres walk the subtrees with a single query, but CTEs are still somewhat uncommon in rails lands.
The problem grows purely with the number of taxonomies assigned to the user. Here I have a user with 337 organizations and 1801 locations.
Before
After