K
Kysely9mo ago
Ross

Deferred Join

Hi - I'm experimenting with adding deferred join capability (as described here: (https://planetscale.com/learn/courses/mysql-for-developers/examples/deferred-joins) to a limit/offset query. The premise is to apply the order by, limit and offset values to the inner join query but I'm having trouble finding a way syntactically to apply this to the inner join. Do I need to use the sql escape hatch? If so, I'm also struggling with where I'd apply that too! Any pointers appreciated as always, thanks
Deferred joins
Optimize your pagination strategy with deferred joins! This video shows you how to modify the offset/limit style to keep it performant as you reach deeper pages. Improve your MySQL skills today.
4 Replies
koskimas
koskimas9mo ago
Something like this? https://kyse.link/?p=s&i=wFay4V59GwY9i0hrlQEA using id is the same as saying person.id = person2.id
Ross
Ross9mo ago
Thank you - I'd just managed to come to a similar point but what I'm struggling with now is generating the order by part dynamically, and then applying that. I'm just putting together a playground to illustrate Here it is - https://kyse.link/?p=s&i=QnDGj00PaNmXsVjRUfDv
koskimas
koskimas9mo ago
The type changes when you call as. You can't assign it to the same variable https://kyse.link/?p=s&i=Jod1DIR88Jmr9SNbIe7R But this won't work
(join) => join.onRef('activityId', '=', 'activityId'))
(join) => join.onRef('activityId', '=', 'activityId'))
you're comparing a column to itself. You need to specify the tables.
(join) => join.onRef('activities.activityId', '=', 'inner_act.activityId'))
(join) => join.onRef('activities.activityId', '=', 'inner_act.activityId'))
Your example can actually be simplified to this https://kyse.link/?p=s&i=gYmIsmgyajbPEGjrR1DM
Ross
Ross9mo ago
Fantastic, thank you so much Worked a treat and sped up my two queries as hoped. Thanks again.