fix: Route admin change page timeout (N+1 in inline integration dropdowns)#425
Closed
chrisdoehring wants to merge 1 commit into
Closed
fix: Route admin change page timeout (N+1 in inline integration dropdowns)#425chrisdoehring wants to merge 1 commit into
chrisdoehring wants to merge 1 commit into
Conversation
The Route admin change page rendered two inlines (RouteProvider, RouteDestination), each row showing the `integration` FK as an eager <select> of every Integration. Because Integration.__str__ dereferences `owner` and `type`, each rendered option triggered an N+1 query — per option, per inline row. With thousands of integrations the page issued hundreds of thousands of queries and timed out. Measured on a test route: 247 queries with ~12 integrations, 746 after adding 25 more — linear scaling with the Integration table. Switch the heavy FKs to autocomplete_fields (AJAX search, zero eager options): `integration` on both inlines and `owner`/`configuration` on RouteAdmin. Add search_fields to RouteConfigAdmin so its FK qualifies. Adds integrations/tests/test_admin.py: a scaling regression test (query count no longer grows with the Integration table) and a widget test. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Contributor
Author
|
Closing as a duplicate of #424, which was merged ahead of this and fixes the same Route admin change-page timeout (autocomplete_fields on the inline integration FKs). The bug is resolved on main. |
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.
Problem
The Route admin change page (e.g.
/admin/integrations/route/<id>/change/) fails to load and times out in production.Root cause
RouteAdminmounts two inlines on the M2M through models (RouteProviderInline,RouteDestinationInline). Each inline row renders the through model'sintegrationForeignKey as a plain<select>listing everyIntegration. BecauseIntegration.__str__dereferences related objects:…and the admin's
ModelChoiceFieldqueryset has noselect_related, rendering each<option>fires 2 extra queries (owner,type). That's1 + 2Nqueries per inline row, and there are 8+ rows by default (existing providers + destinations + 3extraeach). With thousands of integrations in production this becomes hundreds of thousands of queries → timeout.This is distinct from the recent
fix-admin-performancework, which addressedCOUNT(*)on changelist views; this is the change form.Measured evidence
Loading a test route's change page:
i.e. ~20 queries added per integration — linear scaling with the Integration table.
Fix
Switch the heavy FKs to
autocomplete_fields, which renders an AJAX search box instead of an eager dropdown (zero options rendered up front, no N+1):integrationon both inlines (IntegrationAdminalready hassearch_fields)owner+configurationonRouteAdmin(OrganizationAdminalready hassearch_fields)search_fieldstoRouteConfigAdminsoconfigurationqualifies for autocompleteTests
New
integrations/tests/test_admin.py:admin-autocompletewidget.manage.py checkpasses (validates theautocomplete_fields/search_fieldswiring).🤖 Generated with Claude Code