Opened 17 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)

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

Download all attachments as: .zip

Change History (6)

by Matt Riggott, 17 years ago

Attachment: user_get_profile.diff added

comment:1 by Gary Wilson <gary.wilson@…>, 17 years ago

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 by davep@…, 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 Philippe Raoult, 17 years ago

Resolution: wontfix
Status: newclosed

This is really a very minor implementation issue.

comment:4 by anonymous, 15 years ago

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 by Alex Gaynor, 14 years ago

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