The referenced “Deferred join technique” however does a single query using an inner join to achieve the same result.
So, if I’m reading this right, “fast_page” could be made even faster by doing an inner join instead, removing the additional query. I’m not sure why they wouldn’t have done that? It would also ensure both queries were run in the same transaction so you don’t experience race conditions. I’m not a rail dev so maybe that’s a given within a request/response process.
As someone else said, I’m surprised the query planner doesn’t account for this already.
These “deferred joins” where you join in your application code are useful for n+1 type issues. The Django orm has a .prefetch_related method [0] enabling you to optimise down to a single additional query where you would have had a secondary query for each row.
0: https://docs.djangoproject.com/en/4.1/ref/models/querysets/#...