#30685: Optimize QuerySet.count() with distinct()
-------------------------------------+-------------------------------------
     Reporter:  Adam Sołtysik        |                    Owner:  nobody
         Type:                       |                   Status:  new
  Cleanup/optimization               |
    Component:  Database layer       |                  Version:  2.2
  (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 Simon Charette):

 The `annotation` part is tracked in #28477 so I'll rename this ticket to
 only mention `distinct`.

 I'm afraid we can't simply clear the values and ordering, else quite bit
 of code in `get_aggregate`
 
[https://github.com/django/django/blob/65e86948b80262574058a94ccaae3a9b59c3faea/django/db/models/sql/query.py#L440-L444
 would be rendundant].

 One case that these changes don't consider is the fact that
 `.values('foo').distinct()` could influence the `.count()`

 e.g.

 {{{#!sql
 SELECT COUNT(*) FROM (SELECT DISTINCT author_id FROM book)
 }}}

 Is not equivalent to

 {{{#!sql
 SELECT COUNT(*) FROM (SELECT DISTINCT book_id FROM book)
 }}}

 I suggest you open a PR with your changes so CI can report what exactly
 that breaks.

 I'm getting three failures locally on SQLite; one related to the
 aforementioned `.values().distinct()` issue, one related to
 `.datetimes().distinct()` and one related to `order_by` spawning a
 multiple valued relationship.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/30685#comment:9>
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/065.16ca13761866a2e2e345ae9d240763e3%40djangoproject.com.

Reply via email to