-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cursor-based pagination postgresql performance issue with desc order #12650
Comments
This does not seem Prisma related. There's a bunch of posts about how pagination with LIMIT and OFFSET degrades when the table grows and if changing the direction of the sort on an indexed column (which default by ASC). Suggestions include to add an ascending and a descending index, use row numbers to seek rather than to offset. An example of such post is: https://stackoverflow.com/questions/34110504/optimize-query-with-offset-on-large-table |
@okomarov cursor-based pagination doesn't suffer or degrade for large tables much because it's doesn't use |
I wonder what the relation between this issue and #11138 is. |
@casey-chow it's the same, here is just simplified to the minimum with possible solution. One thing is that I think we also should test it without skip = 1 to make sure it has no influence. |
@ziimakc offset of 1 will still trigger a full sort of the table even with the cursor based pagination, or am I missing something? |
@okomarov only big offset value will decrease query performance, the more offset is, the more database is forced to read. Because cursor based pagination doesn't use offset more that one it's not a problem here. Just tested without offset 1 and results are the same, so offset is not an issue here:
|
Bug description
For a simple postgresql database table
test
with one columnid
of type pk integer, filled with 1.000.000 rowsdesc
cursor based pagination takes ~300ms whileasc
takes ~0.3ms.How to reproduce
Schema
ASC query
Query:
Generated SQL:
Explain analyze on this query
DESC query
Query:
Generated SQL:
Explain analyze on this query
DESC query with replaced
Test_id_0
to actual value:Interesting thing if we change query little bit and replace
"order_cmp"."Test_id_0"
with it's value, performance gets back on track.Explain analyze on this query
Expected behavior
Cursor-based pagination postgresql performance should be fast for
desc
order, same asasc
.Prisma information
Environment & setup
Prisma Version
The text was updated successfully, but these errors were encountered: