Function similar to Ecto.Query.first/2 that looks for Ecto.Query.N'th/2

I am looking for the Query that is similar to the Ecto.Query.first/2, but fetch out the Nth record, something like Ecto.Query.N’th/2

offset + limit does that. Ecto.Query.first does nothing else, but wrap it in a convenient function.

3 Likes

Any example…?

User |> order_by(asc: :id) |> limit(1) |> offset(10) |> Repo.one

Will retrieve the 10th user as sorted by id.

2 Likes

Please tread carefully if you want to use large values of N here as it can be a real performance issue. It’s not always wrong universally, though, so here’s one resource about it:

Edit: I have no idea why this post was surfaced to me as fresh, sorry for the necro-reply, I didn’t see timestamps until after I replied.

1 Like

Yeah agreed. Do you know of a solid solution that makes it easy to do keyset pagination AND the ability to sort the whole collection by many different columns?