Find the request unit charge for operations executed in Azure Cosmos DB for MongoDB
APPLIES TO: MongoDB
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 DB 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 it's 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 for MongoDB. If you're using a different API, see API for NoSQL, API for Cassandra, API for Gremlin, and API for Table articles to find the RU/s charge.
The RU charge is exposed by a custom database command named
getLastRequestStatistics. The command returns a document that contains the name of the last operation executed, its request charge, and its duration. If you use the Azure Cosmos DB for MongoDB, you have multiple options for retrieving the RU charge.
Use the Azure portal
Sign in to the Azure portal.
Create a new Azure Cosmos DB account and feed it with data, or select an existing account that already contains data.
Go to the Data Explorer pane, and then select the container you want to work on.
Select the ... next to the container name and select New Query.
Enter a valid query, and then select Execute Query.
Select Query Stats to display the actual request charge for the request you executed. This query editor allows you to run and view request unit charges for only query predicates. You can't use this editor for data manipulation commands such as insert statements.
To get request charges for data manipulation commands, run the
getLastRequestStatisticscommand from a shell based UI such as Mongo shell, Robo 3T, MongoDB Compass, or a VS Code extension with shell scripting.
When you use the Mongo shell, you can execute commands by using runCommand().
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
- Trying to do capacity planning for a migration to Azure Cosmos DB? You can use information about your existing database cluster for capacity planning.
- If all you know is the number of vCores and servers in your existing database cluster, read about estimating request units using vCores or vCPUs
- If you know typical request rates for your current database workload, read about estimating request units using Azure Cosmos DB capacity planner