#15819: Admin searches should use distinct, if query involves joins -------------------------------------+------------------------------------- Reporter: Adam Kochanowski | Owner: ryankask <aip@…> | Status: reopened Type: Bug | Version: master Component: contrib.admin | Resolution: Severity: Normal | Triage Stage: Accepted Keywords: | Needs documentation: 0 Has patch: 1 | Patch needs improvement: 0 Needs tests: 0 | UI/UX: 0 Easy pickings: 0 | -------------------------------------+-------------------------------------
Comment (by akaariai): Yeah, I agree about distinct being slow, and as mentioned in the article it is usually a sign of badly written query. I find it hard to believe doing the distinct operation on Python side would lead to a net win. Databases are written in C and they will be faster at doing the distinct than transferring the whole dataset to Python and then doing the distinct on that side. Making the ORM to do transformation of m2m filter to subquery should be easy in the trivial cases, as we already do them for m2m exclude joins. The harder problem is doing them for queries like this: {{{ qs.filter(Q(m2m_rel__foo='Bar')|Q(m2m_rel__foo='Baz')) }}} The problem is that lookups inside one filter should target the same join (IIRC), but the transform-to-subquery logic doesn't know anything about that. Still, as a long term goal it would be nice to move away from the current confusing situation where filters to m2m can create duplicate results. However, doing that cleanly in the ORM is hard, and then there are backwards compatibility issues too. There is a closely related issue: use aggregations and m2m filters in the same query -> wrong results. So, for the time being, I think we will just need to use the ORM to detect when to use distinct. It should be a small step forward. I hope we can have automatic subqueries, but this isn't likely to happen in the close future. -- Ticket URL: <https://code.djangoproject.com/ticket/15819#comment:20> 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 post to this group, send email to django-updates@googlegroups.com. To unsubscribe from this group, send email to django-updates+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.