1

I'm working on a fairly standard Django app and lately I've been tweaking the admin. While the list views load at reasonable rates, the detail and add views take some ~10 seconds to load.

I'm not exactly sure why it is so-- I suspected it had something to do with the database being over queried, and in fact it is. After turning on logging I discovered that the controller is querying for every id in the table:

...
[Mon Oct 22 18:32:52 2012] [error] (0.001) SELECT `courses_course`.`id`, `courses_course`.`semester`,
 `courses_course`.`name`, `courses_course`.`credits`, `courses_course`.`description`, `courses_course
`.`history_id`, `courses_course`.`oldpcr_id`, `courses_course`.`primary_alias_id` FROM `courses_cours
e` WHERE `courses_course`.`id` = 2077 ; args=(2077,)
[Mon Oct 22 18:32:52 2012] [error] (0.000) SELECT `courses_course`.`id`, `courses_course`.`semester`,
 `courses_course`.`name`, `courses_course`.`credits`, `courses_course`.`description`, `courses_course
`.`history_id`, `courses_course`.`oldpcr_id`, `courses_course`.`primary_alias_id` FROM `courses_cours
e` WHERE `courses_course`.`id` = 2078 ; args=(2078,)
[Mon Oct 22 18:32:52 2012] [error] (0.001) SELECT `courses_course`.`id`, `courses_course`.`semester`,
 `courses_course`.`name`, `courses_course`.`credits`, `courses_course`.`description`, `courses_course
`.`history_id`, `courses_course`.`oldpcr_id`, `courses_course`.`primary_alias_id` FROM `courses_cours
e` WHERE `courses_course`.`id` = 2079 ; args=(2079,)
...

Can somebody explain why this is happening? I can't think of anything at all that might explain it.

Ceasar
  • 22,185
  • 15
  • 64
  • 83
  • How are you tweaking the admin? – César Oct 22 '12 at 22:46
  • Nothing significant-- Just adding ModelAdmins to enable search and filtering and the like. I noticed the effect before I got started so likely someone before me is responsible-- but I can't really guess where to even begin looking. – Ceasar Oct 22 '12 at 22:48
  • 1
    I think I've seen this behavior before. Reason: I wasn't using select_related on my queries – César Oct 22 '12 at 23:02
  • So simply setting that to true doesn't quite help, but that's a helpful lead at least. – Ceasar Oct 22 '12 at 23:09
  • What do you mean by setting it to true? select_related is a QuerySet method. Take a look at [select_related](https://docs.djangoproject.com/en/dev/ref/models/querysets/#select-related) – César Oct 22 '12 at 23:14
  • Er, the admin models have a [setting](https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_select_related) that says whether or not to use them. – Ceasar Oct 22 '12 at 23:22
  • Sorry, yes you are right. For a minute I forgot this is about the admin – César Oct 22 '12 at 23:24
  • This looks relevant: http://stackoverflow.com/questions/9719662/django-admin-performance-issue. It looks like one of the models is probably in some way. – Ceasar Oct 22 '12 at 23:37

1 Answers1

1

I discovered the answer here: Improving Performance of Django ForeignKey Fields in Admin

Because Django lists foreign keys as a select list, it has to query every one. it looks like several solutions are possible, but the simplest is just to set the field to a raw_id field. (And in fact, doing so fixed my problem.)

Community
  • 1
  • 1
Ceasar
  • 22,185
  • 15
  • 64
  • 83