#10060: Multiple table annotation failure
-------------------------------------+-------------------------------------
Reporter: svsharma@… | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Adam Sołtysik):
It's very surprising that aggregations aren't done in subqueries. The
problem arises even with basic, one-field aggregations, when there are
duplicates in the original query. So something like this, with a single
reverse foreign key relation:
{{{
User.objects.filter(orders__date=today).distinct().values('first_name').annotate(Count('pk'))
}}}
may produce wrong results (will count orders instead of users), even
though it looks safe with `.distinct()`. From what I've found, the
documentation doesn't warn about such cases.
Funnily enough, when I have rewritten one of my not working queries with
sum aggregation to use a subquery, it actually became faster (on a table
with ~100k rows), but of course YMMV.
--
Ticket URL: <https://code.djangoproject.com/ticket/10060#comment:70>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/081.47c34554437d931983ab20eb43f33b18%40djangoproject.com.