On Mar 28, 3:52 pm, Fabian Büchler <[email protected]> wrote: > 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?
The easiest way to see which indexes help is to get the generated SQL of the query and then play with PostgreSQL directly in dbshell, or if you want a GUI, use PgAdmin. You can get the sql with str(queryset.query). Then try to create different indexes so that the query will be as fast as possible. I do not know what kind of SQL the above code will generate, but even in the worst case (the generated SQL is suboptimal) this should be relatively easy to write in raw SQL if needed. If you post the generated SQL here I will try to help find the right indexes. But I must say that having a runtime of 15 seconds for so little data feels like the generated query could be suboptimal, or there could be something else strange going on, like the PostgreSQL statistics for the tables being off. Did you load the data just before running this query? If that is the case try to run "vacuum analyze;" in dbshell before testing. I did a little test and without any indexes I could get a runtime of 100 milliseconds for a hand written query doing essentially the same thing. And this is using an old laptop... - Anssi -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

