我正在使用 cosmos db 进行这样的简单检索
public async Task<bool> FindIfDuplicateAsync(string instructionId)
{
var queryDefinition = new QueryDefinition("Select c.evolveId FROM c where c.instructionId = @instructionId")
.WithParameter("@instructionId", instructionId);
var response = await FindAllItemsAsync<MinimalEvolvePaymentRequest>(queryDefinition, null);
if (response.Count == 0)
return false;
return true;
}
FindAllItemsAsync 的定义如下
protected virtual async Task<List<T>> FindAllItemsAsync<T>(QueryDefinition queryDefinition, QueryRequestOptions requestOptions = null, CancellationToken cancellationToken = default(CancellationToken))
{
List<T> documents = new List<T>();
using (FeedIterator<T> feedIterator = Container.GetItemQueryIterator<T>(queryDefinition, null, requestOptions))
{
while (feedIterator.HasMoreResults)
{
documents.AddRange(await feedIterator.ReadNextAsync(cancellationToken));
}
}
return documents;
}
在应用程序洞察中,我看到这样的日志,其中一些需要几秒钟才能完成。通常检索速度非常快,但在某些情况下,它看起来像这样:
https://{{cosmosResource}}.documents.azure.com:443/
http://169.254.169.254/metadata/instance?api-version=2020-06-01
https://{{cosmosResource}}-eastus.documents.azure.com/dbs/payments/colls/paymentRequests
https://{{cosmosResource}}-eastus.documents.azure.com/dbs/payments/colls/paymentRequests/docs
https://{{cosmosResource}}-eastus.documents.azure.com/dbs/WXteAA==/colls/WXteAIy5we0=/pkranges
https://{{cosmosResource}}-eastus.documents.azure.com/dbs/WXteAA==/colls/WXteAIy5we0=/pkranges
https://{{cosmosResource}}-eastus.documents.azure.com//addresses/?$resolveFor=dbs%2fWXteAA%3d%3d%2fcolls%2fWXteAIy5we0%3d%2fdocs&$filter=protocol eq rntbd&$partitionKeyRangeIds=0
https://{{cosmosResource}}.documents.azure.com:443/
https://{{cosmosResource}}-eastus.documents.azure.com/dbs/payments/colls/partnerLedger
https://{{cosmosResource}}-eastus.documents.azure.com/dbs/payments/colls/partnerLedger/docs
https://{{cosmosResource}}-eastus.documents.azure.com/dbs/WXteAA==/colls/WXteALWILLw=/pkranges
https://{{cosmosResource}}-eastus.documents.azure.com/dbs/WXteAA==/colls/WXteALWILLw=/pkranges
https://{{cosmosResource}}-eastus.documents.azure.com//addresses/?$resolveFor=dbs%2fWXteAA%3d%3d%2fcolls%2fWXteALWILLw%3d%2fdocs&$filter=protocol eq rntbd&$partitionKeyRangeIds=0
https://{{cosmosResource}}.documents.azure.com:443/
https://{{cosmosResource}}.documents.azure.com:443/
https://{{cosmosResource}}.documents.azure.com:443/
https://{{cosmosResource}}.documents.azure.com:443/
https://{{cosmosResource}}.documents.azure.com:443/
https://{{cosmosResource}}.documents.azure.com:443/
https://{{cosmosResource}}.documents.azure.com:443/
https://{{cosmosResource}}.documents.azure.com:443/
https://{{cosmosResource}}.documents.azure.com:443/
https://{{cosmosResource}}.documents.azure.com:443/
https://{{cosmosResource}}.documents.azure.com:443/
https://{{cosmosResource}}.documents.azure.com:443/
https://{{cosmosResource}}.documents.azure.com:443/
https://{{cosmosResource}}.documents.azure.com:443/
https://{{cosmosResource}}.documents.azure.com:443/
https://{{cosmosResource}}.documents.azure.com:443/
https://{{cosmosResource}}.documents.azure.com:443/
https://{{cosmosResource}}.documents.azure.com:443/
https://{{cosmosResource}}.documents.azure.com:443/
https://{{cosmosResource}}.documents.azure.com:443/
https://{{cosmosResource}}.documents.azure.com:443/
https://{{cosmosResource}}.documents.azure.com:443/
https://{{cosmosResource}}.documents.azure.com:443/
https://{{cosmosResource}}.documents.azure.com:443/
https://{{cosmosResource}}.documents.azure.com:443/
https://{{cosmosResource}}.documents.azure.com:443/
在常规通话中,日志如下所示:
https://{{cosmosResource}}-eastus.documents.azure.com/dbs/payments/colls/paymentRequests/docs
https://{{cosmosResource}}-eastus.documents.azure.com/dbs/payments/colls/partnerLedger/docs
为什么会发生这种情况,这些额外的
GET
电话来自哪里?
文档有一个非分层分区键,并且没有 RU 限制
这是直接连接初始化的一部分。
https://{{cosmosResource}}.documents.azure.com:443/
以上是SDK获取Account配置(哪些地区,哪些一致性等)。它在初始化期间发生一次,并在后台刷新时每 5 分钟发生一次。
https://{{cosmosResource}}-eastus.documents.azure.com/dbs/payments/colls/paymentRequests
这种情况在初始化期间发生一次,它会获取容器信息(哪个分区键定义路径+其他东西)。
https://{{cosmosResource}}-eastus.documents.azure.com/dbs/WXteAA==/colls/WXteAIy5we0=/pkranges
这种情况在初始化期间发生一次(当时可能有 2 个调用),它会获取物理分区列表。只有当物理分区分裂时才会再次发生。
https://{{cosmosResource}}-eastus.documents.azure.com//addresses/?$resolveFor=dbs%2fWXteAA%3d%3d%2fcolls%2fWXteAIy5we0%3d%2fdocs&$filter=protocol eq rntbd&$partitionKeyRangeIds=0
在分区初始化期间,这种情况会发生一次,如第一个链接中所述。它正在获取分区的 TCP 地址以建立 TCP 连接。
假设您使用CosmosClient
作为单例(您没有为每个查询创建它,这将是错误),那么其中大多数不应为每个操作重复。在您的屏幕截图和日志中,我多次看到
/pkranges
和
/addresses
,这意味着您没有遵循单例模式。