Skip to content

perf: speed up UserAdmin change page inline#1418

Open
thomasrockhu-codecov wants to merge 1 commit into
mainfrom
th/user-admin-change-perf
Open

perf: speed up UserAdmin change page inline#1418
thomasrockhu-codecov wants to merge 1 commit into
mainfrom
th/user-admin-change-perf

Conversation

@thomasrockhu-codecov

Copy link
Copy Markdown
Contributor

Summary

The Django admin User change 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: AccountsUsersInline 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.

This PR:

  • Adds autocomplete_fields = ("account",) to AccountsUsersInline so account choices load on demand (mirrors AccountsUsersAdmin, InvoiceBillingAdmin, StripeBillingAdmin).
  • Sets extra = 0 so no blank widget-bearing form is built on every view.
  • Adds 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:

Before After
Full-table Account scans 6 0
Wall time ~607 ms ~100 ms
Queries 12 10

Remaining account queries are bounded WHERE id= label lookups for selected rows.

Audit (same anti-pattern elsewhere)

Checked every other codecov_auth change page: OwnerAdmin (bot/account already autocomplete, user readonly), InvoiceBillingInline/StripeBillingInline (only FK is the parent link), OwnerUserInline/OwnerOrgInline/Account.users (readonly), AccountAdmin/SentryUserAdmin/OwnerExportAdmin (readonly/no big FK). Only AccountsUsersInline was affected.

Test plan

  • Existing codecov_auth/tests/test_admin.py User/Account/inline tests pass (19 passed)
  • Local profile confirms 0 full-table Account scans post-fix
  • Verify in staging admin that the account field is an autocomplete and still saves correctly
  • Watch Sentry codecov_auth/user/{id}/change/ p95 after deploy

Made with Cursor

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>
@thomasrockhu-codecov thomasrockhu-codecov force-pushed the th/user-admin-change-perf branch from d936771 to 2e72681 Compare July 4, 2026 03:59
@codecov

codecov Bot commented Jul 4, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.89%. Comparing base (701e991) to head (2e72681).
✅ All tests successful. No failed tests found.

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           
Flag Coverage Δ
apiunit 94.94% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@codecov-notifications

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ All tests successful. No failed tests found.

📢 Thoughts on this report? Let us know!

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

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.

1 participant