Search code examples
azureazure-cosmosdbazure-cosmosdb-sqlapi

Cosmos DB OFFSET LIMIT (NoSQL query) RU cost


In the official CosmosDB documentation, we can find information about the inefficient OFFSET LIMIT clause.

"The RU charge of a query with OFFSET LIMIT increases as the number of terms being offset increases. For queries that have multiple pages of results, we typically recommend using continuation tokens."

Docs

Here is link: https://learn.microsoft.com/en-us/azure/cosmos-db/nosql/query/offset-limit#remarks

Not sure if this Cosmos DB documentation is out of date, or just incorrect.

But I have evidence that using OFFSET LIMIT does not load each skipped item & the RU cost is negligible.

Below are screenshots to prove the results.

OFFSET 0, LIMIT 10: 3.22 RUs

OFFSET 500, LIMIT 10: 3.12 RUs

OFFSET 5,000, LIMIT 10: 3.35 RUs

OFFSET 160,000, LIMIT 10: 3.29 RUs

If anyone can clarify this, please share your thoughts.

I tried to find more information about pagination, and in all the sources I found, the continuation token is the most preferable way to implement pagination in CosmosDB. I want to understend why my results contradict the documentation.


Solution

  • The documentation looks out of date.

    The passages you highlighted ...

    The RU charge of a query with OFFSET LIMIT will increase as the number of terms being offset increases ... On the backend, OFFSET LIMIT still loads each document, including those that are skipped

    ... were added in Jan 2020

    This Blog from June 2022 announces

    OFFSET LIMIT optimizations

    The query engine will now be able to avoid document loading time and parsing time for documents outside of the offset range