Find the request unit charge for operations executed in Azure Cosmos DB SQL API
APPLIES TO:
SQL API
Azure Cosmos DB supports many APIs, such as SQL, MongoDB, Cassandra, Gremlin, and Table. Each API has its own set of database operations. These operations range from simple point reads and writes to complex queries. Each database operation consumes system resources based on the complexity of the operation.
The cost of all database operations is normalized by Azure Cosmos DB and is expressed by Request Units (or RUs, for short). Request charge is the request units consumed by all your database operations. You can think of RUs as a performance currency abstracting the system resources such as CPU, IOPS, and memory that are required to perform the database operations supported by Azure Cosmos DB. No matter which API you use to interact with your Azure Cosmos container, costs are always measured by RUs. Whether the database operation is a write, point read, or query, costs are always measured in RUs. To learn more, see the request units and its considerations article.
This article presents the different ways you can find the request unit (RU) consumption for any operation executed against a container in Azure Cosmos DB SQL API. If you are using a different API, see API for MongoDB, Cassandra API, Gremlin API, and Table API articles to find the RU/s charge.
Currently, you can measure this consumption only by using the Azure portal or by inspecting the response sent back from Azure Cosmos DB through one of the SDKs. If you're using the SQL API, you have multiple options for finding the RU consumption for an operation against an Azure Cosmos container.
Use the Azure portal
Sign in to the Azure portal.
Create a new Azure Cosmos account and feed it with data, or select an existing Azure Cosmos account that already contains data.
Go to the Data Explorer pane, and then select the container you want to work on.
Select New SQL Query.
Enter a valid query, and then select Execute Query.
Select Query Stats to display the actual request charge for the request you executed.
Use the .NET SDK
Objects that are returned from the .NET SDK v2 expose a RequestCharge property:
ResourceResponse<Document> fetchDocumentResponse = await client.ReadDocumentAsync(
UriFactory.CreateDocumentUri("database", "container", "itemId"),
new RequestOptions
{
PartitionKey = new PartitionKey("partitionKey")
});
var requestCharge = fetchDocumentResponse.RequestCharge;
StoredProcedureResponse<string> storedProcedureCallResponse = await client.ExecuteStoredProcedureAsync<string>(
UriFactory.CreateStoredProcedureUri("database", "container", "storedProcedureId"),
new RequestOptions
{
PartitionKey = new PartitionKey("partitionKey")
});
requestCharge = storedProcedureCallResponse.RequestCharge;
IDocumentQuery<dynamic> query = client.CreateDocumentQuery(
UriFactory.CreateDocumentCollectionUri("database", "container"),
"SELECT * FROM c",
new FeedOptions
{
PartitionKey = new PartitionKey("partitionKey")
}).AsDocumentQuery();
while (query.HasMoreResults)
{
FeedResponse<dynamic> queryResponse = await query.ExecuteNextAsync<dynamic>();
requestCharge = queryResponse.RequestCharge;
}
Use the Java SDK
Objects that are returned from the Java SDK expose a getRequestCharge() method:
RequestOptions requestOptions = new RequestOptions();
requestOptions.setPartitionKey(new PartitionKey("partitionKey"));
Observable<ResourceResponse<Document>> readDocumentResponse = client.readDocument(String.format("/dbs/%s/colls/%s/docs/%s", "database", "container", "itemId"), requestOptions);
readDocumentResponse.subscribe(result -> {
double requestCharge = result.getRequestCharge();
});
Observable<StoredProcedureResponse> storedProcedureResponse = client.executeStoredProcedure(String.format("/dbs/%s/colls/%s/sprocs/%s", "database", "container", "storedProcedureId"), requestOptions, null);
storedProcedureResponse.subscribe(result -> {
double requestCharge = result.getRequestCharge();
});
FeedOptions feedOptions = new FeedOptions();
feedOptions.setPartitionKey(new PartitionKey("partitionKey"));
Observable<FeedResponse<Document>> feedResponse = client
.queryDocuments(String.format("/dbs/%s/colls/%s", "database", "container"), "SELECT * FROM c", feedOptions);
feedResponse.forEach(result -> {
double requestCharge = result.getRequestCharge();
});
For more information, see Quickstart: Build a Java application by using an Azure Cosmos DB SQL API account.
Use the Node.js SDK
Objects that are returned from the Node.js SDK expose a headers subobject that maps all the headers returned by the underlying HTTP API. The request charge is available under the x-ms-request-charge key:
const item = await client
.database('database')
.container('container')
.item('itemId', 'partitionKey')
.read();
var requestCharge = item.headers['x-ms-request-charge'];
const storedProcedureResult = await client
.database('database')
.container('container')
.storedProcedure('storedProcedureId')
.execute({
partitionKey: 'partitionKey'
});
requestCharge = storedProcedureResult.headers['x-ms-request-charge'];
const query = client.database('database')
.container('container')
.items
.query('SELECT * FROM c', {
partitionKey: 'partitionKey'
});
while (query.hasMoreResults()) {
var result = await query.executeNext();
requestCharge = result.headers['x-ms-request-charge'];
}
For more information, see Quickstart: Build a Node.js app by using an Azure Cosmos DB SQL API account.
Use the Python SDK
The CosmosClient object from the Python SDK exposes a last_response_headers dictionary that maps all the headers returned by the underlying HTTP API for the last operation executed. The request charge is available under the x-ms-request-charge key:
response = client.ReadItem(
'dbs/database/colls/container/docs/itemId', {'partitionKey': 'partitionKey'})
request_charge = client.last_response_headers['x-ms-request-charge']
response = client.ExecuteStoredProcedure(
'dbs/database/colls/container/sprocs/storedProcedureId', None, {'partitionKey': 'partitionKey'})
request_charge = client.last_response_headers['x-ms-request-charge']
For more information, see Quickstart: Build a Python app by using an Azure Cosmos DB SQL API account.
Next steps
To learn about optimizing your RU consumption, see these articles:
- Request units and throughput in Azure Cosmos DB
- Optimize provisioned throughput cost in Azure Cosmos DB
- Optimize query cost in Azure Cosmos DB
- Globally scale provisioned throughput
- Provision throughput on containers and databases
- Provision throughput for a container
- Monitor and debug with metrics in Azure Cosmos DB
Povratne informacije
Pošalјite i prikažite povratne informacije za