Skip to content

Agency: Preload agency ancestors in the API to fix N+1 query#2576

Open
Tschuppi81 wants to merge 4 commits into
masterfrom
performance/n+1-query-api-agency
Open

Agency: Preload agency ancestors in the API to fix N+1 query#2576
Tschuppi81 wants to merge 4 commits into
masterfrom
performance/n+1-query-api-agency

Conversation

@Tschuppi81

@Tschuppi81 Tschuppi81 commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Agency: Preload agency ancestors in the API to fix N+1 query

Building the html link of each agency renders its full path, which walks up the parent chain. Only the direct parent was eager loaded, so every ancestor above it was loaded with its own query. Preload the ancestor closure in bulk instead.

TYPE: Performance
LINK: https://seantis-gmbh.sentry.io/issues/7426519446

@codecov

codecov Bot commented Jul 13, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.43%. Comparing base (31aa86e) to head (f31eb57).
⚠️ Report is 1 commits behind head on master.
✅ All tests successful. No failed tests found.

Additional details and impacted files
Files with missing lines Coverage Δ
src/onegov/agency/api.py 98.08% <100.00%> (+0.41%) ⬆️

... and 1 file with indirect coverage changes


Continue to review full report in Codecov by Harness.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 31aa86e...f31eb57. Read the comment docs.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Daverball

Copy link
Copy Markdown
Member

We probably have the same issue for the /api/topics and /api/news endpoints. This is a problem with all models deriving from AdjacencyList. Perhaps it would be better to cache path on the model. We would just need to be careful, that the cache gets updated for all the descendant nodes when moving parent nodes around. But a simple observer on parent and name should get us there, just like we have an observer for maintaining alphabetic sorting of the siblings.

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.

2 participants