使用以下代码,我尝试将实体插入到 azure 表中。
public void AddItems<T>(T[] entitis, string tableName) where T : TableServiceEntity
{
using (new TimeLogger(_logger, "save items in table " + tableName + ", count: " + entitis.Length + ", duration: {0}"))
{
tableName = GetSafeTableName(tableName);
var items = RemoveDuplicates(entitis).ToArray();
var tasks = new List<Task>();
var context = _tableStorage.GetDataServiceContext();
for (int i = 0; i < items.Length; i++)
{
context.AttachTo(tableName, items[i]);
context.UpdateObject(items[i]);
if (i % 10 == 0 || i == items.Length - 1)
{
var context1 = context;
var task = Task.Factory.FromAsync<DataServiceResponse>(
context1.BeginSaveChangesWithRetries,
context1.EndSaveChangesWithRetries,
context1);
tasks.Add(task);
context = _tableStorage.GetDataServiceContext();
}
}
Task.WaitAll(tasks.ToArray());
}
}
此方法的调用者在多个分区中传递项目。在我的测试中,我发现不同分区中的 450 个项目大约有 7 到 8 秒的延迟。我认为我在这里做错了什么。任何评论都非常感谢。