Opened 12 years ago

Closed 8 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: master
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


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
>>> from django.db import connection
>>> len(connection.queries)

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/ 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)

user_get_profile.diff (745 bytes) - added by Matt Riggott 12 years ago.

Download all attachments as: .zip

Change History (6)

Changed 12 years ago by Matt Riggott

Attachment: user_get_profile.diff added

comment:1 Changed 12 years ago by Gary Wilson <gary.wilson@…>

Triage Stage: UnreviewedDesign decision needed

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).

comment:2 Changed 11 years ago by davep@…

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 Changed 11 years ago by Philippe Raoult

Resolution: wontfix
Status: newclosed

This is really a very minor implementation issue.

comment:4 Changed 10 years ago by anonymous

Resolution: wontfix
Status: closedreopened

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 Changed 8 years ago by Alex Gaynor

Resolution: wontfix
Status: reopenedclosed

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.

Note: See TracTickets for help on using tickets.
Back to Top