Has anyone had difficulty with this type of api call:
projects.annotate(votes_count=Count('votes')).order_by('votes_count')
It’s excruciatingly slow for me and I found this related issue:
https://code.djangoproject.com/ticket/17144
I’m wondering if anyone else is experiencing issues related to this or has good work arounds? It seems like a common enough api call that I’m surprised the bug doesn’t have more traction.
The ticket specifically mentions MySQL. I’m using PostgreSQL and use annotations all the time with no noticeable slowness. However, I’ve never specifically checked the generated SQL to see if it’s doing the same thing.
If you are using MySQL, it would seem you potentially have the following choices (albeit, none of them particular awesome):
Switch over to PostgreSQL. If my anecdotal evidence and the lack of complaints regarding PostgreSQL in the ticket are any indication, it might not be a problem then.
Live with it for now, upgrade to 1.4 when it’s released.
Run off of trunk to get the fix now (not generally a good idea) or try to patch your current installation.
Use
raw()as @jknupp suggests.4 is probably the easiest and best approach for the moment. It’s not really a problem unless you switch database engines at some point in the future, and you can always just make a note to yourself in the code to do it the right way when Django is updated. I typically do something like the following in similar scenarios: