Best way to fetch a range of items

So I’m creating a game using T3+Planetscale where players get a score, I’m currently working on the leaderboard component, where after a player gets a Score, their score is displayed something like: person 1: 3.456 person 2: 3.768 Person 3 :4.523 Current Player:4.789 person 4 :5.123 etc... Basically I need to fetch the 3 players with scores lower/higher than the current score. Not sure how to do that without fetching all the scores. Obviously brute force would be to fetch all the scores and do this client side which is easy, but once their are tons of scores this will lead to a stupid amount of row reads. Any way to write a Prisma query to get around this?
2 Replies
Neto
Neto16mo ago
psudo sql select all scores bigger than X, order the values, take 3 select all scores lesser than X, order the values, take 3
Christoph
Christoph16mo ago
In SQL window functions with the rank() function can be used for leaderboards.
Want results from more Discord servers?
Add your server