#28477: Strip unused annotations from count queries
-------------------------------------+-------------------------------------
     Reporter:  Tom Forbes           |                    Owner:  Simon
         Type:                       |  Charette
  Cleanup/optimization               |                   Status:  assigned
    Component:  Database layer       |                  Version:  dev
  (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 Simon Charette):

 * needs_better_patch:  1 => 0


Comment:

 I submitted [https://github.com/django/django/pull/16263 a revised PR]
 that strip unused annotations not only for `.count()` for but any usage of
 `.aggregate`.

 To take example from comment:7 and comment:9 the resulting query is now

 {{{#!python
 Person.objects.annotate(full_name=Concat('first_name', Value(' '),
 'last_name')).count()
 }}}
 {{{#!sql
 SELECT COUNT(*) FROM support_person
 }}}

 The adjusted logic goes along these lines.

 Instead of systematically performing a subquery wrapping when there are
 existing annotations only do so when the pre-existing annotation are
 aggregate or window functions. In both cases, pre-existing
 aggregation/window or not, strip annotations that are not referenced by
 the aggregates.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/28477#comment:13>
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/010701844c272df4-bc7d30b1-2851-420f-ae77-8dfa6d8b99dc-000000%40eu-central-1.amazonses.com.

Reply via email to