by samwillis · 4y ago · 0 comments
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/#...
> ERROR 1235 (42000): This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
See https://dev.mysql.com/doc/refman/5.7/en/subquery-restriction...
with page_numbers as (
select id from docs order by id limit 25 offset 50
)
select * from docs where id in (
select id from page_numbers
)[1] https://hakibenita.com/be-careful-with-cte-in-postgre-sql
This article is intended for PostgreSQL versions 11 and prior. Starting at version 12, PostgreSQL changed the way it treats CTE to prevent the issues described in this article.
I think for my package (the Laravel one) I'll have to hold off on that because CTEs only work with MySQL 8.0 and up. Also the Laravel query builder doesn't have methods to add CTEs, unfortunately.
I'm gonna noodle on this though, there's something nice about it.
https://dev.mysql.com/doc/refman/8.0/en/lateral-derived-tabl...
Prior to this you can emulate it with ROW NUMBER Window function in subquery, and this is what LATERAL gets turned into in many DB engines I believe
https://stackoverflow.com/questions/73127487/aws-athena-v2-p...
I made that choice for mine because I didn't want to pollute the `select` part of the developer's query to exclude the joined data. If we were writing the whole query from scratch we could, but not in the scenario where the package is actually used, unfortunately.
select * from Car
inner join Color on Car.ColorId = Color.Id
where Color.Name = 'Red'
You can't get the correct paginated list of car id's without doing the join in the first query. Even more wrong if you're ordering off the joined table.