Cursor-based pagination

Are there any plans for cursor-based pagination, and if not, can this be a feature request?

References:

Thanks.

2 Likes

I use cursor based pagination with first:N, after:X, not sure if it ‘not working’ is accurate after a couple years since your second linked post.

Edit: just saw you have a tag of graphql which I do not use at all, so if the question is of the scope of the GraphQL layer only, disregard my input.

The question is for both GraphQL and DQL, though I am focusing on GraphQL. I assume that the ‘X’ you’re referring to is a UID, no? That’s different to using cursors from my understanding.

Doing a search on the Dgraph docs returns no results for ‘cursor’, so I assume they’re not yet implemented.

Cursor based pagination is a method of pagination that uses a pointer to the last result in a page to denote the next page - which is what after:uid does. If you can’t find the word cursor in the docs I think that’s basically an SEO issue. This works fine in DQL afaik.

I guess I’m really talking about from an optimized perspective, and how it’s handled internally.

A cursor would be a separate structure that stores the last position, and starts again at the next position. This will be stored as an independent entity internally, and is designed to be optimized.

Using a UID, while it may give the same result, it may need to scan through the results to get to the relevant UID, and then carry on from there. If it does need to scan through results, then this isn’t going to be as performant as a cursor under ordinary circumstances.

On small data sets, the difference isn’t going to be great, but on very large datasets, it could potentially result in a noticeably large delay.

Ah yes, I understand the question - using after: is far more efficient than using offset: for high values of offset:. Consider offset: having to build the entire result set first+offset and then just cutting off the first offset amount, vs after where a filter is used when collecting results of the form AND uid > ${afterUID}. (This is kind of an elementary explanation)

Do note that after should only be used when no sorting parameter is given, as the default order is by uid value.

https://dgraph.io/docs/query-language/pagination/

1 Like

I’d forgotten about after and not sorting. For unsorted lists, it definitely makes sense to use after: uid.

Depending on how they’re implemented, cursors could help the performance of sorted queries - especially if they’re cached for a certain length of time.