Great work on this, Alexandr. I've been thinking a lot about doing joins in 
Django to subqueries. As you point out, when you are doing several subquery 
annotations of the same subquery, just with different columns, that can 
really hurt performance. Currently, I've been recommending doing a JSON 
object as the subquery column.

I really like the idea of an explicit join to a subquery, as it can perform 
significantly better - in the case where most of the rows share the same 
subquery, the database can perform the subquery once per distinct row 
joined, rather than once for each row.

I think, instead of using a `JoinedSubquery`, we should just make it that a 
`Subquery`, containing unresolved OuterRef instances, can also be resolved 
by doing a `QuerySet.join(Subquery(...))`.

We could also have a lateral argument that allowed a LATERAL JOIN to a 
subquery, but perhaps that's later down the track.

(Actually, now I think about it, a QuerySet should probably just act as a 
Subquery when it's used as one, but perhaps that's too magic).

Matt.

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/2bec9c0d-574c-493a-bbe2-fa58ccccab56%40googlegroups.com.

Reply via email to