Speed up user search indexing by fixing query cache usage for profiles.
Review Request #15013 — Created April 15, 2026 and submitted
User.get_profile()has checks in place to make use of any profile
fetched along with the user viaselect_related()or
prefetch_related().In the case where the cached profile result is a
None, we were falling
back on default behavior based on thecreate_if_missingflag. This is
fine if the flag isTrue(we want to create) but if the flag is
Falsea redundant fetch would occur, which would likely still return a
Noneresult.This code now respects a
Noneentry in the cache in this case, which
will raiseProfile.DoesNotExistas it would have before, but without
the redundant database query.This will significantly speed up search indexing for users, which was
performing 4 queries per user.
Unit tests pass.
Built new unit tests (for an upcoming change) that found this issue and
verified the fix.
| Summary | ID |
|---|---|
| 0d8312240a2242ecc0271ca0a0d8f2e432242e57 |
| Description | From | Last Updated |
|---|---|---|
|
This method now returns None if we've done select_related('profile') and the profile doesn't exist (where previously it would raise DoesNotExist. … |
|
|
|
This should be updated to say that we explicitly do not fetch if the cache variable was already set to … |
|
|
|
local variable 'profile' is assigned to but never used Column: 13 Error code: F841 |
|
|
|
Can we add num_statements to this? |
|
|
|
Can we add num_statements to this? |
|
|
|
Can we add num_statements to this? |
|
-
-
This method now returns
Noneif we've doneselect_related('profile')and the profile doesn't exist (where previously it would raiseDoesNotExist. It would be nice to clarify that here. as well as add a "Version Changed" block. -
This should be updated to say that we explicitly do not fetch if the cache variable was already set to
None
- Change Summary:
-
- Restored the
Profile.DoesNotExisterror. - Clarified the conditions in a comment.
- Restored the
- Description:
-
User.get_profile()has checks in place to make use of any profilefetched along with the user via select_related()orprefetch_related().In the case where the cached profile result is a
None, we were fallingback on default behavior based on the create_if_missingflag. This isfine if the flag is True(we want to create) but if the flag isFalsea redundant fetch would occur, which would likely still return aNoneresult.~ This code now respects a
Noneentry in the cache in this case,~ returning the Nonedirectly without the extra fetch.~ This code now respects a
Noneentry in the cache in this case, which~ will raise Profile.DoesNotExistas it would have before, but without+ the redundant database query. This will significantly speed up search indexing for users, which was
performing 4 queries per user. - Commits:
-
Summary ID a811778bca289d5549c5ab3e0db679d6212b4ef7 42bd585f0143ac18e69067bf74afa9d16f988729
- Change Summary:
-
Removed an unused variable.
- Commits:
-
Summary ID 42bd585f0143ac18e69067bf74afa9d16f988729 0d8312240a2242ecc0271ca0a0d8f2e432242e57