paginationazure-cosmosdbpartitioningazure-cosmosdb-sqlapi

What is the advised way to query documents from different partitions in CosmosDB?


When using Azure Cosmos DB and querying one partition, i just specify the partition key in the FeedOptions. But when i have to query n partitions, i have (afaik) 2 options:

  1. Run a separate Task for every partition and merge the result in my application code
  2. Set the Flag "EnableCrossPartitionQuery" (along with MaxDegreeOfParallelism) in FeedOoptions and contrain my partitions in the query.

When i have to apply sort criteria along with paging on the whole result set (across all partitions) i think the first approach will reach it's limits.

What is the recommended way to query across multiple partitions in Cosmos DB using the .NET SQL API?


Solution

  • The first approach is not recommended unless you know every possible partition key value that your documents have and you are ready to write some parallel request code. It's only efficient if you wanna query a few partitions but not all.

    Enabling the EnableCrossPartitionQuery is the recommended approach if you want to query all the partitions but ideally, you wanna use it as less as possible.

    CosmosDB knows if the partition key definition is part of the query and will limit it's results to the partitions for this query if the partition key values are provided.

    This means that if you write something like select * from c where c.partitionKey = 'something' || c.partitionKey = 'somethingelse' and you enable the EnableCrossPartitionQuery options, your query will be executed only against the 2 partitions that are part of your query (something and somethingelse).