#30685: Suboptimal QuerySet.distinct().count()
-------------------------------------+-------------------------------------
     Reporter:  adamsol              |                    Owner:  nobody
         Type:                       |                   Status:  closed
  Cleanup/optimization               |
    Component:  Database layer       |                  Version:  2.2
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:  invalid
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by adamsol):

 I don't think it's a bug, but I think there is a place for optimization. I
 can't see a reason why all columns are SELECTed in a subquery just to
 perform a simple count. I've attached the SQLs generated and what I would
 expect.

 So, for any model, a query like this:

 {{{Model.objects.distinct().count()}}}

 should produce an SQL like this:

 {{{SELECT COUNT(*) FROM (SELECT DISTINCT "table"."id" AS Col1 FROM
 "table") subquery;}}}

 instead of:

 {{{SELECT COUNT(*) FROM (SELECT DISTINCT "table"."id" AS Col1, ... (all
 other columns) FROM "table") subquery;}}}

 which, depending on the number of columns and size of the table, can be
 several times slower (at least on PostgreSQL).

 If you think this is not worth the effort, then OK, I won't insist.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/30685#comment:2>
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 django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/065.c601f2474d248b860127577aef97a8b3%40djangoproject.com.

Reply via email to