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

适用于: SQL API

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 SQL API 中的容器执行的任何操作所消耗的请求单位 (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 SQL API. 如果你使用的是其他 API,请参阅 API for MongoDBCassandra APIGremlin API表 API 这几篇文章来查找 RU/秒费用。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.

目前,若要度量这种消耗,只能使用 Azure 门户,或者通过某个 SDK 检查 Azure Cosmos DB 发回的响应。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. 如果使用 SQL API,则可以使用多个选项来查找针对 Azure Cosmos 容器执行的操作所消耗的 RU。If you're using the SQL API, you have multiple options for finding the RU consumption for an operation against an Azure Cosmos container.

使用 Azure 门户Use the Azure portal

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

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

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

  4. 选择“新建 SQL 查询”。Select New SQL 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 门户中的 SQL 查询请求费用屏幕截图

使用 .NET SDKUse the .NET SDK

.NET SDK v2 返回的对象公开 RequestCharge 属性: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;
}

使用 Java SDKUse the Java SDK

Java SDK 返回的对象公开 getRequestCharge() 方法: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();
});

有关详细信息,请参阅快速入门:使用 Azure Cosmos DB SQL API 帐户生成 Java 应用程序For more information, see Quickstart: Build a Java application by using an Azure Cosmos DB SQL API account.

使用 Node.js SDKUse the Node.js SDK

Node.js SDK 返回的对象公开 headers 子对象,该对象可映射底层 HTTP API 返回的所有标头。Objects that are returned from the Node.js SDK expose a headers subobject that maps all the headers returned by the underlying HTTP API. 请求费用显示在 x-ms-request-charge 键下: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'];
}

有关详细信息,请参阅快速入门:使用 Azure Cosmos DB SQL API 帐户生成 Node.js 应用For more information, see Quickstart: Build a Node.js app by using an Azure Cosmos DB SQL API account.

使用 Python SDKUse the Python SDK

Python SDK 返回的 CosmosClient 对象公开 last_response_headers 字典,该字典可映射底层 HTTP API 针对上次执行的操作返回的所有标头。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. 请求费用显示在 x-ms-request-charge 键下: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']

有关详细信息,请参阅快速入门:使用 Azure Cosmos DB SQL API 帐户生成 Python 应用For more information, see Quickstart: Build a Python app by using an Azure Cosmos DB SQL API account.

后续步骤Next steps

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