duy
duy

Reputation: 579

Should the query contains partition key if we have set partition key into FeedOption already?

I am using Document DB with partition key = "deviceId". Is there any different between 2 code below:

var fo = new FeedOption{ PartitionKey= new PartitionKey("A1234") };
var partitionKeyInQuery= dbClient.CreateDocumentQuery(d => d.deviceId = "A1234" and d.type==1, fo);
var noPartitionKeyInQuery = dbClient.CreateDocumentQuery(d => d.type==1, fo);

When PartitionKey is applied in FeedOption, should I add "deviceId" in WHERE clause?

Upvotes: 5

Views: 755

Answers (1)

Peppi Alexandrova
Peppi Alexandrova

Reputation: 401

I believe there is no difference in performance. RequestCharge is the same and the where clause makes the query partition specific i.e eliminates cross partition query.

From the documentation:

Querying partitioned containers

When you query data in partitioned containers, Cosmos DB automatically routes the query to the partitions corresponding to the partition key values specified in the filter (if there are any). For example, this query is routed to just the partition containing the partition key "XMS-0001".

// Query using partition key
IQueryable<DeviceReading> query = client.CreateDocumentQuery<DeviceReading>(
    UriFactory.CreateDocumentCollectionUri("db", "coll"))
    .Where(m => m.MetricType == "Temperature" && m.DeviceId == "XMS-0001");

https://learn.microsoft.com/en-us/azure/cosmos-db/sql-api-partition-data

Upvotes: 3

Related Questions