symposion icon indicating copy to clipboard operation
symposion copied to clipboard

Slot __unicode__ method hits the DB hard

Open spacedman opened this issue 11 years ago • 4 comments

Am trying to get my head round Symposion (see postings to the google group...).

I created 195 Slot objects for my conference, and some admin pages became very slow - such as creating a new Presentation.

The debug toolbar showed it was doing nearly 1000 database queries - or 5 times the number of slots.

Repeatable with a smaller set of Slots.

Traced it back to the unicode method of Slot - in getting the string rep of Slot for the drop-down in the Presentation admin field, it has to get the string rep of the Day, which causes another db hit, which then gets the schedule, which probably then gets the Conference object all from the DB. If I replaced Slot.unicode to just return a simple string of the start and end time the the DB hits went down to the number of Slots as expected.

Either I'm doing it wrong and having 195 slots isn't the right thing (we have 195 presentations, so I need 195 slots, yes? See google group post for details) or Django is being very inefficient. This is all running off the debug runserver.

What I might prefer would be for slots to have a CharField name...

spacedman avatar May 06 '13 16:05 spacedman

:+1: It may get better to fix following way.

  1. Defines slot name
  2. update it when saving and definition is same as __unicode__
  3. return name instead of current implementation when called __unicode__

miurahr avatar Jul 16 '15 05:07 miurahr

This way?

class Slot(models.Model):

    (snip)

    name = models.CharField(max_length=100, editable=False)

    def save(self, *args, **kwargs):
        self.name = "%s %s (%s - %s)" % (self.day, self.kind, self.start, self.end)
        super(Slot, self).save(*args, **kwargs)

    def __unicode__(self):
        return self.name

miurahr avatar Jul 25 '15 06:07 miurahr

I don't see how this reduces the queries.

paltman avatar Aug 03 '15 12:08 paltman

Let's assume a next case:

  1. save slots
  2. show slots in admin list

before-patch case

  1. save slots, insert query once
  2. happen 4 queries per one slot. If 100 slots, 400 queries.

after-patch case

  1. save slots, insert query and update a self.name field.
  2. happen 1 query per one slot. If 100 slots, 100 queries.

A problem is a situation when listing slots, the patch take effect. It make more query when save but it is only once when save.

miurahr avatar Aug 03 '15 14:08 miurahr