symposion
symposion copied to clipboard
Slot __unicode__ method hits the DB hard
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...
:+1: It may get better to fix following way.
- Defines slot
name
- update it when saving and definition is same as
__unicode__
- return name instead of current implementation when called
__unicode__
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
I don't see how this reduces the queries.
Let's assume a next case:
- save slots
- show slots in admin list
before-patch case
- save slots, insert query once
- happen 4 queries per one slot. If 100 slots, 400 queries.
after-patch case
- save slots, insert query and update a self.name field.
- 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.