Search code examples
javaazure-cosmosdbdatabase-partitioningazure-cosmosdb-sqlapi

Difference between partition key indicated in query expression and feedoptions in cosmos db


I'm calling "queryDocuments(String collectionLink, String query, FeedOptions options)" api in cosmos db for doing some sql query with partition key. I wonder where I should indicate the partition key, query string or feedoptions?

In terms of performance consideration, I wonder if I should indicate partition key in feedoptions options parameter

Like the difference queryDocuments(collectionLink, "SELECT * FROM root WHERE id = xxx AND partitionkey = XXX", null); or feedoptions.setpartitionkey(PK); queryDocuments(collectionLink, "SELECT * FROM root WHERE id = xxx", feedoptions);

Thanks for your answer!


Solution

  • I suggest you indicate partition key in FeedOptions.I did a micro test to observe the performance of both of the solutions.

    First One:

    String name = "A";
    FeedResponse<Document> feedResponse = client
          .queryDocuments("dbs/db/colls/part",
                            "SELECT * FROM c WHERE c.name ='" + name + "'", null);
    System.out.println(feedResponse.getRequestCharge());
    

    Second One:

    FeedOptions queryOptions = new FeedOptions();
    PartitionKey partitionKey = new PartitionKey("/A");
    queryOptions.setPartitionKey(partitionKey);
    FeedResponse<Document> feedResponse1 = client
          .queryDocuments("dbs/db/colls/part",
                        "SELECT * FROM c ", queryOptions);
    System.out.println(feedResponse1.getRequestCharge());
    

    Test Data:

    enter image description here

    Output:

    enter image description here

    Test shows the second one consumes lower RUs than the first one.My sample data is too small, the gap between them will increase with the increase of the data, I presume.