[p2pu-dev] A little SQL/Django ORM riddle to start off the week!

Dirk Uys dirk at p2pu.org
Mon Jan 7 07:55:02 UTC 2013


Hi everyone

I've been investigating some of the SQL queries that's really slow. I've
noticed that we are using quite a few queries that uses sub-queries -
"where value1 in (select value 2 from ... )

So, as a first example (and also one of the slowest queries):

We have this function:
https://github.com/p2pu/lernanta/blob/master/lernanta/apps/projects/models.py#L323-L334

that generate this SQL: https://gist.github.com/4473163

I've rewritten the query using only joins to achieve the same (faster):
https://gist.github.com/4473181

So the riddle: how do you get Django's ORM to generate something more like
the second query?

Cheers
Dirk
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.p2pu.org/pipermail/p2pu-dev/attachments/20130107/b9c00020/attachment.html>


More information about the p2pu-dev mailing list