我有一个运行在Azure上的.NET函数应用程序,它将数据上传到CosmosDB,如下所示:
foreach (var message in messages)
{
try
{
await notificationsContainer.UpserItemAsync(message, message.NTID);
}
catch (Exception exception)
{
//...
}
}UpsertItemAsync是一个包装器:
public async Task<T> UpsertItemAsync(T document, string partitionKey)
{
ItemResponse<T> response = await _container.UpsertItemAsync<T>(document, new PartitionKey(partitionKey));
return response.Resource;
}我在做6500条短信的测试。上传640(!)花了16分钟。发送到数据库的消息。同时,使用Python的CosmosClient,这个调用
container.create_item(message)
乘以6500,需要131秒才能完成。
此外,函数应用程序运行在Azure上,CosmosClient设置为直接连接模式:
CosmosClient client = clientBuilder
.WithConnectionModeDirect()
.WithThrottlingRetryOptions(new TimeSpan(0, 0, 0, 0, config.MaxRetryWaitTimeInMilliSeconds), config.MaxRetryCount)
.WithBulkExecution(true)
.Build();当python脚本在on上运行时。
如何解释这种表现上的巨大差异?这个函数应用程序是不是太慢了?
发布于 2021-04-26 13:54:50
您的问题是,您正在启用大容量模式(.WithBulkExecution(true)),但对每个操作执行await。
当使用大容量模式(引用https://devblogs.microsoft.com/cosmosdb/introducing-bulk-support-in-the-net-sdk/)时,您需要创建这些操作,而不是单独等待。类似于:
List<Task> operations = new List<Task>();
foreach (var message in messages)
{
operations.Add(notificationsContainer.UpserItemAsync(message, message.NTID));
}
try
{
await Task.WhenAll(operations);
}
catch(Exception ex)
{
//...
}如果要执行单独的操作执行,可以禁用大容量模式。
https://stackoverflow.com/questions/67267567
复制相似问题