如何监视 Azure Cosmos DB 中的操作的吞吐量或请求单位用量How to monitor throughput or request unit usage of an operation in Azure Cosmos DB

适用于 Azure Cosmos DB 的 Azure Monitor 提供一个指标视图,用于监视帐户和创建仪表板。Azure Monitor for Azure Cosmos DB provides a metrics view to monitor your account and create dashboards. 默认情况下,系统会收集 Azure Cosmos DB 指标,此功能不需要你显式启用或配置任何设置。The Azure Cosmos DB metrics are collected by default, this feature does not require you to enable or configure anything explicitly. “请求单位总数”指标用于获取不同类型的操作的请求单位用量 。The Total Request Units metric is used to get the request units usage for different types of operations. 以后,你可以分析哪些操作使用了大部分吞吐量。Later you can analyze which operations used most of the throughput. 默认情况下,吞吐量数据是按一分钟间隔聚合的。By default, the throughput data is aggregated at one-minute interval. 但是,可以通过更改时间粒度选项来更改聚合单位。However, you can change the aggregation unit by changing the time granularity option.

可通过两种方式分析请求单位用量数据:There are two ways to analyze the request unit usage data:

  • 在给定时间间隔内,哪些操作占用了较多的请求单位。Within the given time interval which operations are taking more request units.
  • 一般而言,哪些操作通过消耗较多的请求单位支配了工作负荷。Which operations in general dominate your workload by consuming more request units. 这种分析使你可以专注于插入、更新插入等操作并查看其索引编制。This analysis allows you to focus on operations such as insert, upsert and look at their indexing. 可以查明特定字段的索引编制是否过度/不足,并修改索引策略以包含或排除路径。You can find out if you are over/under indexing specific fields and modify the indexing policy to include or exclude the paths.

如果发现某些查询占用了较多的请求单位,可以采取如下所述的措施:If you notice certain queries are taking more request units, you can take actions such as:

  • 重新考虑请求的数据量是否适当。Reconsider if you are requesting the right amount of data.
  • 修改查询以使用带有筛选子句的索引。Modify the query to use index with filter clause.
  • 执行开销更低的 UDF 函数调用。Perform less expensive UDF function calls.
  • 定义分区键,以便最大程度地减少将查询扇出到不同的分区中的情况。Define partition keys to minimize the fan out of query into different partitions.
  • 还可以使用调用响应中返回的查询指标以及诊断日志详细信息,并参考查询性能优化一文来详细了解查询执行。You can also use the query metrics returned in the call response, the diagnostic log details and refer to query performance tuning article to learn more about the query execution.
  • 可以从总和开始,然后使用适当的维度查看平均利用率。You can start from sum and then look at avg utilization using the right dimension.

查看请求单位用量指标总计View the total request unit usage metric

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

  2. 在左侧导航栏中选择“监视”,然后选择“指标”。 Select Monitor from the left-hand navigation bar and select Metrics.

    Azure Monitor 中的“指标”窗格

  3. 在“指标”窗格中选择一个资源,然后选择所需的订阅和资源组。 From the Metrics pane > Select a resource > choose the required subscription, and resource group. 对于“资源类型”,请选择“Azure Cosmos DB 帐户”,选择一个现有的 Azure Cosmos 帐户,然后选择“应用”。 For the Resource type, select Azure Cosmos DB accounts, choose one of your existing Azure Cosmos accounts, and select Apply.

    选择 Azure Cosmos DB 帐户以查看指标

  4. 接下来,从可用指标的列表中选择“请求单位总数”指标。 Next select the Total Request Units metric from the list of available metrics. 若要详细了解此列表中的所有可用指标,请参阅按类别划分的指标一文。To learn in detail about all the available metrics in this list, see the Metrics by category article. 在此示例中,让我们选择“请求单位总数”和“平均”作为聚合值。 In this example, let's select Total Request Units and Avg as the aggregation value. 除这些详细信息外,还可以选择指标的“时间范围”和“时间粒度”。 In addition to these details, you can also select the Time range and Time granularity of the metrics. 可以查看过去最长 30 天的指标。At max, you can view metrics for the past 30 days. 应用筛选器后,系统会根据该筛选器显示图表。After you apply the filter, a chart is displayed based on your filter. 可以查看所选时间段内每分钟消耗的平均请求单位数。You can see the average number of request units consumed per minute for the selected period.

    从 Azure 门户中选择指标

请求单位用量筛选器Filters for request unit usage

还可以按特定的 CollectionName、DatabaseName、OperationType、Region、Status 和 StatusCode 筛选指标并显示相应的图表。 You can also filter metrics and get the charts displayed by a specific CollectionName, DatabaseName, OperationType, Region, Status, and StatusCode. 使用“添加筛选器”和“应用拆分”选项可以筛选请求单位用量并将指标分组。 The Add filter and Apply splitting options allows you to filter the request unit usage and group the metrics.

若要按总计(总和)或平均值获取每个操作的请求单位用量,请选择“应用拆分”,然后选择“操作类型”和筛选器值,如下图所示: To get the request unit usage of each operation either by total(sum) or average, select Apply splitting and choose Operation type and the filter value as shown in the following image:

Azure Monitor 中的操作的 Cosmos DB 请求单位

若要按集合查看请求单位用量,请选择“应用拆分”并选择集合名称作为筛选器。 If you want to see the request unit usage by collection, select Apply splitting and choose the collection name as a filter. 在仪表板中将会看到如下所示的图表,其中包含所选的集合。You will see a chat like the following with a choice of collections within the dashboard. 然后,可以选择特定的集合名称以查看更多详细信息:You can then select a specific collection name to view more details:

Azure Monitor 中按集合列出的所有操作的 Cosmos DB 请求单位

后续步骤Next steps