With CosmosDb do queries that specify a PartitionKey have the returned results limited to an exact match on the PartitionKey if not specified in the WHERE clause

Don Gillett 20 Reputation points
2025-02-17T22:28:26.3333333+00:00

In CosmosDb I store documents that have a RowKey that maps to an EventId. I also have a dedicated field called PartitionKey which is a concatenation of StoreID and a day (day portion of DateTime). This makes it so that if I need to get all the events for a single day I just need to hit two partitions to account for the local time spanning 2 GMT days. This means our largest stores are spread out across partitions over time and this seems to work good for that.

My question is when I use the DotNet library and I pass in my desired PartitionKey, is my query limited to return results just for that exact value of PartitionKey. My concern is that I know PartitionKey is often hashed (or looked up) to form which server to actually go to... but once it gets to the correct physical partition is Partition Key automatically added to an implied WHERE clause? Or do I need to also add WHERE PartitionKey = @PartionKey and send it in as part of the query as well.

Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,768 questions
0 comments No comments
{count} votes

Accepted answer
  1. Marcin Policht 36,435 Reputation points MVP
    2025-02-17T22:48:15.95+00:00

    Yep - AFAIK, when you pass the PartitionKey in your query using the .NET SDK for Cosmos DB, it automatically restricts the query to only return results from that exact partition. You do not need to explicitly add WHERE PartitionKey = @PartitionKey in your query.

    Cosmos DB internally routes your request to the correct physical partition based on the provided PartitionKey. The query execution is scoped to that partition, so the filtering is implicitly applied.


    If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.

    hth

    Marcin

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Don Gillett 20 Reputation points
    2025-02-17T22:56:49.5733333+00:00

    Thanks for your reply. "query execution is scoped to that partition".. . But there is other data contained in the same physical partition. To be more precise do you mean "query execution is scoped to that LOGICAL partition"? My concern is that any time I've asked this question I've never been given a definitive answer that hasn't had elements leading uncertainty or ambiguity. I've deep dived into the docs and I appreciate the answer but the AFAIK preface and fact that scoping to a physical partition wouldn't produce the right results still leaves me uncertain (but certainly better than I was before).


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.