I'm using django's (1.8.5) conditional annotation in order to add filtered data to each object dynamically. But the issue is that it's very slow on all machines I've tested it.
What it does is basically adds data to each user. How many messages they sent from provided date, how many messages have failed and how many messages they sent from yesterday. There is not much data. Just about 100 users and each of them may have about 50 messages daily. objects = self.get_queryset().annotate(sent_count= Sum( Case(When(messages__date_sent__gte=date_from, then=1)), output_field=IntegerField() ), error_count= Sum( Case(When(messages__status__iexact='error', then=1)), output_field=IntegerField() ), sent_yesterday= Sum( Case(When(messages__date_sent__gte=yesterday, then=1)), output_field=IntegerField() ) ) When checking a query, it takes up to 15 seconds to execute it. Am I doing something wrong or is conditional annotation not designed for things like this? After looking at the generated query, it seems that Django is trying to GROUP BY by every available field in both models and their related models.. After adding prefetch_related to some related models, issue was fixed on one machine, but still persists on the second one. Is this a bug? -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/75d15495-9c66-4e3c-a153-912b220d9e35%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.