Opened 18 years ago
Closed 14 years ago
#4196 closed (wontfix)
[patch] Use select_related() when retrieving a user profile
Reported by: | Matt Riggott | Owned by: | nobody |
---|---|---|---|
Component: | Contrib apps | Version: | dev |
Severity: | Keywords: | ||
Cc: | Triage Stage: | Design decision needed | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
To start with an example:
>>> from django.contrib.auth.models import User >>> u = User.objects.get(pk=1) >>> p = u.get_profile() >>> p.user.username 'dave' >>> from django.db import connection >>> len(connection.queries) 3
Even though I'm dealing with two database objects (a User and its profile) three queries were executed. The first to get the user, the second to get the profile, and the third to get the user again.
If line 257 in django/contrib/auth/models.py was changed to use select_related
the database would only be hit twice.
It's a minor thing, but I thought I'd mention it. You know, just in case you guys ran out of patches to test and such.
(If a user profile has lots of foreign key fields this could result in a lot of objects being created, which is why I've limited the depth to 1.)
Attachments (1)
Change History (6)
by , 18 years ago
Attachment: | user_get_profile.diff added |
---|
comment:1 by , 18 years ago
Triage Stage: | Unreviewed → Design decision needed |
---|
comment:2 by , 17 years ago
Just to add my ill-informed opinion as a Django newbie - I arrived here while googling trying to find out how to apply select_related to get_profile. Obviously you don't, yet. Is there anything wrong with adding a default flag ... get_profile(related=true) ... type thing?
comment:3 by , 17 years ago
Resolution: | → wontfix |
---|---|
Status: | new → closed |
This is really a very minor implementation issue.
comment:4 by , 16 years ago
Resolution: | wontfix |
---|---|
Status: | closed → reopened |
Is it possible to have a get_profile(related=True) sort of implementation wherein based on the value of related, we can have get_profile getting either select_related or not?
I understand is was closed as very minor, but this does help quite a bit and I do not want to change the django code in my system for this.
The problem is that when a new version comes, we have to remember putting it again.
comment:5 by , 14 years ago
Resolution: | → wontfix |
---|---|
Status: | reopened → closed |
Using select_related makes no sense here, we already know the user object to be fetched, filling in the cache automatically would be a saner implementation.
Yeah I'm not sure what would be best here. The larger query using
select_related()
may be the same or worse than the one additional query that would be saved (especially with several foreign keys in the user profile).