I have a postgresql query that does pagination using a keyset-based approach.
SELECT *
FROM public.values_with_variable_view
WHERE ( timestamp, id) > ('2024-01-04 01:12:09.267335+00',1996)
ORDER BY ( timestamp, id) asc
LIMIT 100
I have a requirement to write this using LINQ in c#. I tried using the where clause with .Where(x=>x.timestamp > '2024-01-04 01:12:09.267335+00' && x.id > 1996)
but it gives a different result.
Here is DBFiddle
2
Answers
You should add
||
part:I would recommend the
.Where(x => x.timestamp >= last.timestamp && (x.timestamp > last.timestamp || x.id > last.id))
form for potentially better performance. Having thex.timestamp >= last.timestamp
condition up front with no alternative or condition should promote better index usage. The second part of the condition refines the filter after the initial index seek.Combined with the code from Svyatoslav Danyliv’s post, the answer would be:
See this updated fiddle.
Note that I have added an index for
(timestamp, id)
on the test table.The real determination as to which is best should be to try both options against live data (presumably substantially larger than 25 rows) and review the execution plans for performance.