Monday, March 28, 2011

Slow query. Any way to speed things up?

Hello everyone,

maybe someone can help me to speed up a quite slow query:

I've got two models (only the important stuff outlined here): Events and EventDates

class Event(models.Model):
    status = models.SmallIntegerField(verbose_name=_(u"status"),
        choices=STATUS_CHOICES, default=STATUS_ONLINE, db_index=True)
   
    online = EventOnlineManager()


class EventDate(models.Model):
    event = models.ForeignKey(Event)
   
    date = models.DateField(verbose_name=_(u"start date"), db_index=True)


Events have an EventOnlineManager with a "to_expire" method which should select all Events with status=online and EventDates associated which date < today.

class EventOnlineManager(models.Manager):
  
    def get_query_set(self):
        return (super(EventOnlineManager, self).get_query_set()
                    .filter(status=Event.STATUS_ONLINE))
   
    @property
    def to_expire(self):
        today = datetime.date.today()
        return (self.annotate(eventdate_max=models.Max('eventdate__date'))
                    .filter(eventdate_max__lt=today))

Now this query takes about 15 seconds to run on a database with about 5,000 Events and 50,000 EventDates.
I'm running Django 1.3 (trunk) and PostgreSQL 9.0 on a relatively recent quadcore machine.

Is there any way to do a more efficient query?

Thanks in advance!
Regards, Fabian

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment