Re: Optimizing out unused annotations from count queries

2017-08-21 Thread Adam Johnson
I'm not sure how much QuerySet optimization django should really be doing. Databases' optimizers are really good and as we've seen there are a huge number of edge cases in trying to do the work in Djangoland, as QuerySets don't really correspond to the underlying data model. Here's an example in M

Re: Optimizing out unused annotations from count queries

2017-08-21 Thread Anssi Kääriäinen
On Monday, August 21, 2017 at 11:28:35 AM UTC+3, Tom Forbes wrote: > > > Interestingly enough, just doing a .filter(m2m_relation__foo='bar') > might change the results > > Is that not because it's a filter? Would ". > annotate(x=F('m2m_relation__foo'))" change the results in any way? Sorry if >

Re: Optimizing out unused annotations from count queries

2017-08-21 Thread Tom Forbes
> Interestingly enough, just doing a .filter(m2m_relation__foo='bar') might change the results Is that not because it's a filter? Would ". annotate(x=F('m2m_relation__foo'))" change the results in any way? Sorry if I'm not following. On 21 Aug 2017 06:58, "Anssi Kääriäinen" wrote: On Sunday,

Re: Optimizing out unused annotations from count queries

2017-08-20 Thread Anssi Kääriäinen
On Sunday, August 20, 2017 at 2:48:23 AM UTC+3, Josh Smeaton wrote: > Thanks for making me elaborate, because I'm probably wrong. I'll step > through what my thinking was though. > > '.annotate(local=F('related__field'))' will join to the related table. If > `related` is a nullable foreign key,

Re: Optimizing out unused annotations from count queries

2017-08-19 Thread Josh Smeaton
Thanks for making me elaborate, because I'm probably wrong. I'll step through what my thinking was though. '.annotate(local=F('related__field'))' will join to the related table. If `related` is a nullable foreign key, then a join would do an implicit filter, removing the row from the result set

Re: Optimizing out unused annotations from count queries

2017-08-19 Thread Tom Forbes
Thanks for your reply Josh. Can you elaborate on why optimizing out ' .annotate(local=F('related__field'))' would not be safe? On 20 Aug 2017 00:10, "Josh Smeaton" wrote: I'd like to see this provided all bases were covered. I'll just list below the cases where I think it wouldn't be safe. - Fi

Re: Optimizing out unused annotations from count queries

2017-08-19 Thread Josh Smeaton
I'd like to see this provided all bases were covered. I'll just list below the cases where I think it wouldn't be safe. - Filtered annotations - Annotations that join to anything other than a non-null foreign key: .annotate(local=F('related__field')) - Annotations that have a GROUP BY on fields