#33655: Unnecessary column in a GROUP BY clause with QuerySet.exists()
-------------------------------------+-------------------------------------
     Reporter:  Marc Perrin          |                    Owner:  nobody
         Type:                       |                   Status:  new
  Cleanup/optimization               |
    Component:  Database layer       |                  Version:  4.0
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  exists group by      |             Triage Stage:  Accepted
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

 * cc: Simon Charette (added)
 * type:  Uncategorized => Cleanup/optimization
 * stage:  Unreviewed => Accepted


Comment:

 Thanks for the report. I agree that `(1)` is unnecessary in the `GROUP BY`
 clause unfortunately we cannot always remove constants from it because
 folks can use them as an alias for n-th column (as you already noticed).
 Maybe we could select only the first column from the `GROUP BY` clause
 instead:
 {{{#!diff
 diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
 index 64e7927f7a..940d3141d1 100644
 --- a/django/db/models/sql/query.py
 +++ b/django/db/models/sql/query.py
 @@ -582,8 +582,11 @@ class Query(BaseExpression):
          q.clear_ordering(force=True)
          if limit:
              q.set_limits(high=1)
 -        q.add_extra({"a": 1}, None, None, None, None, None)
 -        q.set_extra_mask(["a"])
 +        if q.group_by and not q.select:
 +            q.add_select_col(q.group_by[0], "a")
 +        else:
 +            q.add_extra({"a": 1}, None , None, None, None, None)
 +            q.set_extra_mask(["a"])
          return q

      def has_results(self, using):
 }}}
 Tentatively accepted for future investigation.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/33655#comment:1>
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/010701804aec58f5-ab4ac646-e561-4385-9505-48229bf38243-000000%40eu-central-1.amazonses.com.

Reply via email to