1👍
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:
"""Django has a bug in version 1.3 (see ticket:
https://code.djangoproject.com/ticket/17144), resulting in unnecessary fields
being included in the GROUP BY clause and subsequently very slow queries. The raw
query below can be replaced with the commented line once the issue has been
corrected.
"""
# projects.annotate(votes_count=Count('votes')).order_by('votes_count')
projects.raw(...)
1👍
To get around this, you can use the raw()
SQL query. It’s not the prettiest way to do it, but it will work. See the documentation here
- [Answered ]-Django: Insert new row with 'order' value of the next highest value avoiding race condition
- [Answered ]-Writing a custom Django BaseCommand class that logs the command details
- [Answered ]-Should I use class based or function based views for this?