I'm trying to page a query in Azure Cosmos DB
by reading this post: > Paging through query results in Azure DocumentDB , I got to the code:
public async Task <ICollection <TEntity>> GetAllPaged(int page, string colletionId) {
FeedOptions queryOptions = new FeedOptions {
MaxItemCount = 10
};
var query = client.CreateDocumentQuery <TEntity> (
UriFactory.CreateDocumentCollectionUri(databaseId, colletionId), queryOptions)
.AsDocumentQuery();
FeedResponse <TEntity> result;
List <TEntity> tEntitys = new List <TEntity> ();
int interacoes = 0;
while (query.HasMoreResults) {
interacoes++;
if (interacoes == page) {
result = await query.ExecuteNextAsync <TEntity> ();
tEntitys = result.ToList();
break;
}
}
return tEntitys;
}
Explaining:
-
MaxItemCount = 10
, total items per page. -
while (query.HasMoreResults)
, execute aloop
until results are available. -
if (interacoes == page)
, when I get to my page I no longer need to check the next ones, wherebreak
.
Problem:
Imagine that I have about 50002 documents, and that my page asks the method for the result number 5001, that is, the last 2 records.
Each interaction of while
this code: result = await query.ExecuteNextAsync<TEntity>()
is responsible for each results page.
I thought of executing this code only on the specified page that I need through this code: if (interacoes == page)
, the problem is that only the first result is read, ie I need to read page by page to achieve the desired result described in scenario above, and this has generated me 15 seconds of performance.
Note: Skip
is not supported because it performs poorly. More in the GitHub Issues !