Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR fixes prisma/prisma#11138, and fixes prisma/prisma#12650
Might be related to prisma/prisma#16235, prisma/prisma#17328, and probably more
The implementation of how
cursor
is implemented before is to create a subquery, store it as a temporary table, and to be used to compare with the values in the tableBut the issue with that is that the database would try to use nested loop to join the temporary table and the main table, which causes it to scan through everything before it until it found a match
The explanation can be found in this stackoverflow question and in prisma/prisma#12650+issuecomment-1099064362 you can see that
Index Only Scan
fetched999,504
rows andJoin Filter
removed999,500
rowsThis PR fixes this issue by fetching the individual column to be compared direclty without creating a temporary table
Here's the difference between the generated query:
And here's the difference running these queries with
explain (analyze,verbose)
running postgres using HDD:On a more complex query, it might fetch the same column multiple times, but that's still faster than trying to scan through everything before it