Cosmos Db stored procedure RU charge is higher than equivalent query via sdk or portal?

Viewed 858

Is it expected that a query executed via a stored proc has a higher RU charge (almost double), than that of the same query executed via the sdk or portal?

This test code shows both cases execute a query for a single document against partitionKey and document id.

Simple query executed via stored proc

Simple stored proc to test execution of a single document by id. As this is executed withing the scope of a single partition, it should result in a direct query against partition and doc id.

function testProc(id, props) {
    var collection = getContext().getCollection();
    var collectionLink = collection.getSelfLink();
    var response = getContext().getResponse();

    // Validate input.
    if (!id) throw new Error("The id is undefined or null.");
    if (!props) throw new Error("The update is undefined or null.");

    tryQuery();

    function tryQuery() {
         var query = {query: "select * from root r where r.id = @id", parameters: [{name: "@id", value: id}]};

         var isAccepted = collection.queryDocuments(collectionLink, query, function (err, documents, responseOptions) {
             if (err) throw err;

             if (documents.length == 1) {
                 // Update props
             } else {
                 // Else a document with the given id does not exist..
                 throw new Error("Document not found.");
             }
         });

         // If we hit execution bounds - throw an exception.
         // This is highly unlikely given that this is a query by id; but is included to serve as an example for larger queries.
         if (!isAccepted) {
             throw new Error("The stored procedure timed out.");
         }
     }
}

Request Charge: 5.68 RU

Query direct in the portal

SELECT * FROM c where c.partitionKey = 'Tenant_8_u768f5c6b-ef6d-4cd9-8ad0-ae0c68eb9887@mail.com' and c.id = 'c12a3dbd-ad19-41d6-971b-79e0c7acec41'

Request Charge: 2.890 RUs

I can only assume according to the cosmos request units and throughput documentation that this discrepancy comes about due to the physical overhead of compute resources (cpu / memory) required in order to run the stored proc itself.

For a simple query to pull out a document by partition and id it seems rather high though.

0 Answers
Related