#27719: Add queryset.alias() to mimic .annotate() for aggregations without loading data -------------------------------------+------------------------------------- Reporter: Marc Tamlyn | Owner: Alexandr | Tatarinov Type: New feature | Status: assigned Component: Database layer | Version: master (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 -------------------------------------+------------------------------------- Changes (by Alexandr Tatarinov):
* owner: nobody => Alexandr Tatarinov * status: new => assigned Comment: I am working on this one. I've had a lot of cases where unneeded expressions ended up in a select clause. When someone builds complex queries spanning multiple methods and modules, this can save a lot of CPU (for ORM to build the query) and query execution time. My suggestion is to support only the usages below, values(), distinct() and aggregate() will require to use .annotate() to add the expression into the select list. {{{ .alias(alias=SomeExpression()).annotate(SomeOtherExpression('alias')) .alias(alias=SomeExpression()).filter(alias__lookup=value) .alias(alias=SomeExpression()).order_by('alias') .alias(alias=SomeExpression()).update(field=F('alias')) }}} Also automatically supported: earliest/latest and dates/datetimes. And if you want to use values() or distinct(), promote alias to annotation {{{ .alias(alias=SomeExpression()).annotate(alias=F('alias')).distinct('alias') }}} The implementation relies on annotation_mask to remove an expression from the select list, all other logic remains the same: despite the expression is not selected, the joins and group by are the same as for annotation. -- Ticket URL: <https://code.djangoproject.com/ticket/27719#comment:10> 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/066.07805586b8dcbfe95257c9e7b4e38571%40djangoproject.com.