#25470: Serious performance impact (500x slower!) when querying distinct model
dates objects
-------------------------------------+-------------------------------------
     Reporter:  stanleyxu2005        |                    Owner:  felixxm
         Type:                       |                   Status:  assigned
  Cleanup/optimization               |
    Component:  Database layer       |                  Version:  1.8
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:  Accepted
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by felixxm):

 * has_patch:  0 => 1


Comment:

 I made [https://github.com/django/django/pull/5428 PR] to omit unnecessary
 type cast.

 In my opinion, the second step, is to change default {{{order}}} in
 {{{dates}}}, {{{datetimes}}} methods

 {{{#!diff
 --- a/django/db/models/query.py
 +++ b/django/db/models/query.py
 @@ -726,21 +726,25 @@ class QuerySet(object):
          clone._iterable_class = FlatValuesListIterable if flat else
 ValuesListIterable
          return clone

 -    def dates(self, field_name, kind, order='ASC'):
 +    def dates(self, field_name, kind, order=None):
          """
 }}}
 or give a way to disable it
 {{{#!diff
 --- a/django/db/models/query.py
 +++ b/django/db/models/query.py
 @@ -733,14 +733,18 @@ class QuerySet(object):
          """
          assert kind in ("year", "month", "day"), \
              "'kind' must be one of 'year', 'month' or 'day'."
 -        assert order in ('ASC', 'DESC'), \
 -            "'order' must be either 'ASC' or 'DESC'."
 -        return self.annotate(
 +        assert order is None or order in ('ASC', 'DESC'), \
 +            "'order' must be either 'ASC', 'DESC' or None."
 +        qs = self.annotate(
              datefield=Date(field_name, kind),
              plain_field=F(field_name)
          ).values_list(
              'datefield', flat=True
 -        ).distinct().filter(plain_field__isnull=False).order_by(('-' if
 order == 'DESC' else '') + 'datefield')
 +        ).distinct().filter(plain_field__isnull=False)
 +        if order is not None:
 +            return qs.order_by(('-' if order == 'DESC' else '') +
 'datefield')
 +        else:
 +            return qs
 }}}

--
Ticket URL: <https://code.djangoproject.com/ticket/25470#comment:14>
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 post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/071.2c57e7e57988092b879975532bea60bd%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to