如何在 DocumentDB 中测量 RU?

How to measure RU in DocumentDB?

鉴于 Azure DocumentDB 使用请求单位作为吞吐量的衡量标准,我想确保我的查询使用尽可能少的 RU 来增加我的吞吐量。是否有工具可以告诉我一个查询将占用多少 RU,以及该查询是否实际使用了索引?

我发现 DocumentDb Studio 显示了在每个查询中提供 RU 的响应 header。

如您所见,某些工具会在查询完成后提供 RU。这也可以通过编程方式获得,因为 x-ms-request-charge header 在响应中返回,并且可以通过 DocumentDB SDK 轻松检索。

例如,这里有一段显示使用 JS/node 检索 RU 的片段:

var queryIterator = client.queryDocuments(collLink, querySpec );
queryIterator.executeNext(function (err, results, headers) {
  if (err) {
    // deal with error...
  } else {
    // deal with payload...
    var ruConsumed = headers['x-ms-request-charge'];
  }
});

至于你关于索引的问题,以及确定 属性 是否被索引(然后应该回答你关于使用或不使用索引的查询的问题):你可以查询 collection,其中 returns 响应中的索引详细信息 header。

例如:给定一些路径 dbs/<databaseId>/colls/<collectionId>:

var collLink = 'dbs/' + databaseId+ '/colls/'+ collectionId;

client.readCollection(collLink, function (err, coll) {
    if (err) {
        // deal with error

    } else {
        // compare indexingPolicy with your property, to see if it's included or excluded
        // this just shows you what these properties look like
        console.log("Included: " + JSON.stringify(coll.indexingPolicy.includedPaths))
        console.log("Excluded: " + JSON.stringify(coll.indexingPolicy.excludedPaths))
    }
});

您会看到 includedPathsexcludedPaths 看起来像这样,然后您可以按照您认为合适的任何方式搜索给定的 属性:

Included: [{"path":"/*","indexes":[{"kind":"Range","dataType":"Number","precision":-1},{"kind":"Hash","dataType":"String","precision":3}]}]
Excluded: []

另一种选择是在启用跟踪收集选项的情况下使用模拟器。 https://docs.microsoft.com/en-us/azure/cosmos-db/local-emulator

我试图分析 LINQ 聚合查询,目前这似乎无法使用 c# SDK。

使用模拟器的跟踪输出,我能够识别请求费用和许多其他指标。有很多数据需要处理。 我找到了存储在这个事件密钥下的请求费用 DocDBServer/Transport_Channel_Processortask/Genericoperation

示例输出:
ThreadID="141,928" FormattedMessage="EndRequest DocumentServiceId localhost, ResourceType 2, OperationType 15, ResourceId 91M7AL+QPQA=, StatusCode 200, HRESULTHex 0, ResponseLength 61, Duration 70,546, HasQuery 1, PartitionId a4cb495b-38c8-11e6-8106-8cdcd42c33be, ReplicaId 1, ConsistencyLevel 3, RequestSessionToken 0:594, ResponseSessionToken 594, HasContinuation 0, HasPreTrigger 0, HasPostTrigger 0, IsFeedUnfiltered 0, IndexingDirective 5, XDate Fri, 09 Jun 2017 08:49:03 GMT, RetryAfterMilliseconds 0, MaxItemCount -1, ActualItemCount 1, ClientVersion 2017-02-22, UserAgent Microsoft.Azure.Documents.Common/1.13.58.2, RequestLength 131, NetworkBucket 2, SubscriptionId 00000000-0000-0000-0000-000000000000, Region South Central US, IpAddress 192.168.56.0, ChannelProtocol RNTBD, RequestCharge 51.424, etc...

这可以与来自另一个包含查询信息的事件的数据相关联: DocDBServer/ServiceModuletask/Genericoperation

请注意,您需要 perfview 才能查看 ETL 日志文件。有关更多信息,请参见此处: https://github.com/Azure/azure-documentdb-dotnet/blob/master/docs/documentdb-sdk_capture_etl.md