Couchbase N1QL OFFSET pagination for large datasets with non-unique sort keys
0 reputation · 16 Nov 2024, 18:47 UTC
Pagination for a large Couchbase dataset is commonly implemented with N1QL ORDER BY, LIMIT, and OFFSET clauses. When the sort attribute is not guaranteed to be unique, each page boundary can become unstable if documents are inserted, updated, or deleted between requests. Large OFFSET values also require the engine to scan and discard preceding rows, which raises latency as the page number grows.
The goal is to bound the result set predictably while keeping the query plan efficient. It is unclear whether N1QL provides a documented mechanism to make OFFSET-based paging stable when the sort key is non-unique, or whether the only supported approach is to switch to keyset pagination with a unique indexed attribute. Version-specific behavior across Couchbase Server 7.x and later also needs confirmation.
Can OFFSET pagination be made deterministic with a non-unique ORDER BY key in current N1QL? Is there a documented performance threshold where OFFSET should be abandoned in favor of keyset filters? What index configuration is required to make either approach efficient for deep paging?