perf: speed up UserAdmin change page inline#1418
Open
thomasrockhu-codecov wants to merge 1 commit into
Open
Conversation
The User admin change page was avg ~7s / p95 ~13.7s in production despite
trivial DB time. The cost was rendering AccountsUsersInline: it had no
autocomplete, so every inline form rendered a <select> of every Account,
loading the whole table (a cursor over all rows) and calling Account.__str__
per option.
Give the inline autocomplete_fields=("account",) so account choices load on
demand, drop extra=1 -> 0 so no blank widget form is built on view, and
select_related("account","user") to avoid an N+1 when rendering existing rows.
Local profile (2000 accounts): full-table Account scans 6 -> 0, wall
~607ms -> ~100ms. Audited the other codecov_auth change pages; every other
editable FK already uses autocomplete or is readonly, so no further changes.
Co-authored-by: Cursor <cursoragent@cursor.com>
d936771 to
2e72681
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1418 +/- ##
=======================================
Coverage 91.89% 91.89%
=======================================
Files 1325 1325
Lines 50868 50871 +3
Branches 1626 1626
=======================================
+ Hits 46744 46747 +3
Misses 3818 3818
Partials 306 306
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! 🚀 New features to boost your workflow:
|
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.
Summary
The Django admin
Userchange page (codecov_auth/user/{id}/change/) is the #3 most expensive admin page in Sentry (avg ~7s, p95 ~13.7s) — but its DB time is trivial (<1s). The cost is rendering:AccountsUsersInlinehad no autocomplete, so every inline form rendered a<select>of everyAccount, loading the whole table (a cursor over all rows) and callingAccount.__str__per option.This PR:
autocomplete_fields = ("account",)toAccountsUsersInlineso account choices load on demand (mirrorsAccountsUsersAdmin,InvoiceBillingAdmin,StripeBillingAdmin).extra = 0so no blank widget-bearing form is built on every view.get_queryset(...).select_related("account", "user")to avoid an N+1 when rendering existing rows.No schema/migration change.
Measured impact
Local profile with 2,000 accounts:
AccountscansRemaining account queries are bounded
WHERE id=label lookups for selected rows.Audit (same anti-pattern elsewhere)
Checked every other
codecov_authchange page:OwnerAdmin(bot/accountalready autocomplete,userreadonly),InvoiceBillingInline/StripeBillingInline(only FK is the parent link),OwnerUserInline/OwnerOrgInline/Account.users(readonly),AccountAdmin/SentryUserAdmin/OwnerExportAdmin(readonly/no big FK). OnlyAccountsUsersInlinewas affected.Test plan
codecov_auth/tests/test_admin.pyUser/Account/inline tests pass (19 passed)codecov_auth/user/{id}/change/p95 after deployMade with Cursor