查找 Azure Cosmos DB API for MongoDB 中执行的操作的请求单位费用Find the request unit charge for operations executed in Azure Cosmos DB API for MongoDB

适用于: Azure Cosmos DB API for MongoDB

Azure Cosmos DB 支持多种 API,例如 SQL、MongoDB、Cassandra、Gremlin 和表。Azure Cosmos DB supports many APIs, such as SQL, MongoDB, Cassandra, Gremlin, and Table. 每个 API 具有自身的数据库操作集。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.

所有数据库操作的成本将由 Azure Cosmos DB 规范化,并以“请求单位”(简称 RU)表示。The cost of all database operations is normalized by Azure Cosmos DB and is expressed by Request Units (or RUs, for short). 你可以将 RU 视为性能货币,它抽象化了执行 Azure Cosmos DB 支持的数据库操作所需的系统资源,例如 CPU、IOPS 和内存。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. 不管使用哪个 API 来与 Azure Cosmos 容器和数据库操作交互,都始终以 RU 来计量成本。No matter which API you use to interact with your Azure Cosmos container, costs are always measured by RUs. 无论数据库操作是写入、点读取还是查询,都始终以 RU 来计量成本。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.

本文介绍如何通过不同的方式,来查找针对 Azure Cosmos DB API for MongoDB 中的容器执行的任何操作所消耗的请求单位 (RU)。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 API for MongoDB. 如果你使用的是其他 API,请参阅 SQL APICassandra APIGremlin API表 API 文章,找到每秒的 RU 费用。If you are using a different API, see SQL API, Cassandra API, Gremlin API, and Table API articles to find the RU/s charge.

RU 费用由名为 getLastRequestStatistics 的自定义数据库命令公开。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. 如果使用 Azure Cosmos DB API for MongoDB,则可以通过多个选项来检索 RU 费用。If you use the Azure Cosmos DB API for MongoDB, you have multiple options for retrieving the RU charge.

使用 Azure 门户Use the Azure portal

  1. 登录到 Azure 门户Sign in to the Azure portal.

  2. 创建新的 Azure Cosmos 帐户并在其中植入数据,或选择一个已包含数据的现有帐户。Create a new Azure Cosmos account and feed it with data, or select an existing account that already contains data.

  3. 转到“数据资源管理器”窗格,然后选择要处理的容器。Go to the Data Explorer pane, and then select the container you want to work on.

  4. 选择“新建查询”。Select New Query.

  5. 输入有效的查询,然后选择“执行查询”。Enter a valid query, and then select Execute Query.

  6. 选择“查询统计信息”,以显示执行的请求的实际请求费用。Select Query Stats to display the actual request charge for the request you executed.

    Azure 门户中的 MongoDB 查询请求费用屏幕截图

使用 MongoDB .NET 驱动程序Use the MongoDB .NET driver

使用官方 MongoDB .NET 驱动程序时,可以通过对 IMongoDatabase 对象调用 RunCommand 方法来执行命令。When you use the official MongoDB .NET driver, you can execute commands by calling the RunCommand method on a IMongoDatabase object. 此方法需要 Command<> 抽象类的实现:This method requires an implementation of the Command<> abstract class:

class GetLastRequestStatisticsCommand : Command<Dictionary<string, object>>
{
    public override RenderedCommand<Dictionary<string, object>> Render(IBsonSerializerRegistry serializerRegistry)
    {
        return new RenderedCommand<Dictionary<string, object>>(new BsonDocument("getLastRequestStatistics", 1), serializerRegistry.GetSerializer<Dictionary<string, object>>());
    }
}

Dictionary<string, object> stats = database.RunCommand(new GetLastRequestStatisticsCommand());
double requestCharge = (double)stats["RequestCharge"];

有关详细信息,请参阅快速入门:使用 Azure Cosmos DB API for MongoDB 生成 .NET Web 应用For more information, see Quickstart: Build a .NET web app by using an Azure Cosmos DB API for MongoDB.

使用 MongoDB Java 驱动程序Use the MongoDB Java driver

使用官方 MongoDB Java 驱动程序时,可以通过对 MongoDatabase 对象调用 runCommand 方法来执行命令:When you use the official MongoDB Java driver, you can execute commands by calling the runCommand method on a MongoDatabase object:

Document stats = database.runCommand(new Document("getLastRequestStatistics", 1));
Double requestCharge = stats.getDouble("RequestCharge");

有关详细信息,请参阅快速入门:使用 Azure Cosmos DB API for MongoDB 和 Java SDK 生成 Web 应用For more information, see Quickstart: Build a web app by using the Azure Cosmos DB API for MongoDB and the Java SDK.

使用 MongoDB Node.js 驱动程序Use the MongoDB Node.js driver

使用官方 MongoDB Node.js 驱动程序时,可以通过对 db 对象调用 command 方法来执行命令:When you use the official MongoDB Node.js driver, you can execute commands by calling the command method on a db object:

db.command({ getLastRequestStatistics: 1 }, function(err, result) {
    assert.equal(err, null);
    const requestCharge = result['RequestCharge'];
});

有关详细信息,请参阅快速入门:将现有的 MongoDB Node.js Web 应用迁移到 Azure Cosmos DBFor more information, see Quickstart: Migrate an existing MongoDB Node.js web app to Azure Cosmos DB.

后续步骤Next steps

若要了解如何优化 RU 消耗量,请参阅以下文章:To learn about optimizing your RU consumption, see these articles: