stibbs
10/20/2021, 9:03 PMstibbs
10/20/2021, 9:09 PMpublic.jobs
table
CURRENT IMPLEMENTATION
Retrieves 100% of the data from public.jobs
that is 'valid' (not closed, validity ends some time in the future), and lists items first if they have remuneration details (remuneration != null). This is the main bit of my current plpgsql function.
sql
...
return query
...
where j.remuneration_min is not null
and j.closed = false
and j.valid_until > now()
...
UNION ALL
...
where j.remuneration_min is null
and j.closed = false
and j.valid_until > now()
...
This setup becomes unwieldy as the number of jobs grows (mostly from a DOM rendering perspective...), resulting in a bad user experience (at ~100 jobs the query takes ~710ms to come back from supabase). As this is on my index page, this results in an unnecessarily long initial render.
SOLUTION..?
I think the solution is the seek method w/ index (based on https://wiki.postgresql.org/images/3/35/Pagination_Done_the_PostgreSQL_Way.pdf) but I'm not sure, and I haven't figured out how to do the index when I need to pre-sort based on remuneration `null`/`not null`stibbs
10/20/2021, 9:22 PMsilentworks
10/20/2021, 9:23 PMstibbs
10/20/2021, 9:23 PMstibbs
10/20/2021, 9:24 PMsilentworks
10/20/2021, 9:24 PMstibbs
10/20/2021, 9:24 PMsilentworks
10/20/2021, 9:25 PMstibbs
10/20/2021, 9:25 PMsilentworks
10/20/2021, 9:25 PMstibbs
10/20/2021, 9:25 PMsilentworks
10/20/2021, 9:26 PMsilentworks
10/20/2021, 9:31 PMLIMIT
to your query and pass in the values to get it working with infinite scrollingstibbs
10/21/2021, 12:10 AMLIMIT
and also to check what the last item was in the previous result set... so it would be something like this (don't think it's valid sql though?)
sql
create or replace function list_jobs(prev_date timestamp with timezone)
...
return query
...
where j.remuneration_min is not null
and j.closed = false
and j.valid_until > now()
...
UNION ALL
...
where j.remuneration_min is null
and j.closed = false
and j.valid_until > now()
...
where valid_until < prev_date
limit 10;
stibbs
10/21/2021, 12:14 AMwhere
) on the result of a UNION ALL
?stibbs
10/21/2021, 3:38 AMlimit
and offset
with those numbers.
So the code would be:
sql
create or replace function list_jobs(
limit_num integer,
offset_num integer
)
...
return query
...
where j.remuneration_min is not null
and j.closed = false
and j.valid_until > now()
...
UNION ALL
...
where j.remuneration_min is null
and j.closed = false
and j.valid_until > now()
...
limit limit_num offset offset_num;