我编写了简单的 WorkerRole,将测试数据添加到表中。插入的代码是这样的。
var TableClient = this.StorageAccount.CreateCloudTableClient();
TableClient.CreateTableIfNotExist(TableName);
var Context = TableClient.GetDataServiceContext();
this.Context.AddObject(TableName, obj);
this.Context.SaveChanges();
此代码针对每个客户端请求运行。我用 1-30 个客户端线程进行测试。 我对不同大小的实例进行了多次尝试。我不知道我做错了什么,但我无法达到每秒 10 次以上的插入速度。 如果有人知道如何提高速度,请告诉我。 谢谢
更新
- 删除 CreateTableIfNotExist 对我的插入测试没有影响。
- 切换模式为expect100Continue="false"useNagleAlgorithm="false"当插入速率跳至30-40 ips时,会产生短暂的效果。但 30 秒后,插入率下降到 6 ips,超时时间为 50%。
最佳答案
为了加快速度,您应该使用批量事务(实体组事务),允许您在单个请求中提交最多 100 个项目:
foreach (var item in myItemsToAdd)
{
this.Context.AddObject(TableName, item);
}
this.Context.SaveChanges(SaveChangesOptions.Batch);
您可以将其与 Partitioner.Create 结合使用(+ AsParallel) 在每批 100 个项目的不同线程/核心上发送多个请求,使事情变得非常快。
但在做这一切之前,read through the limitations使用批量事务(100 个项目,每个事务 1 个分区,...)。
更新:
由于您无法使用交易,这里有一些其他提示。看看this MSDN thread关于提高使用表存储时的性能。我写了一些代码来向您展示差异:
private static void SequentialInserts(CloudTableClient client)
{
var context = client.GetDataServiceContext();
Trace.WriteLine("Starting sequential inserts.");
var stopwatch = new Stopwatch();
stopwatch.Start();
for (int i = 0; i < 1000; i++)
{
Trace.WriteLine(String.Format("Adding item {0}. Thread ID: {1}", i, Thread.CurrentThread.ManagedThreadId));
context.AddObject(TABLENAME, new MyEntity()
{
Date = DateTime.UtcNow,
PartitionKey = "Test",
RowKey = Guid.NewGuid().ToString(),
Text = String.Format("Item {0} - {1}", i, Guid.NewGuid().ToString())
});
context.SaveChanges();
}
stopwatch.Stop();
Trace.WriteLine("Done in: " + stopwatch.Elapsed.ToString());
}
所以,第一次运行它时,我得到以下输出:
Starting sequential inserts.
Adding item 0. Thread ID: 10
Adding item 1. Thread ID: 10
..
Adding item 999. Thread ID: 10
Done in: 00:03:39.9675521
添加 1000 个项目需要超过 3 分钟。现在,我根据MSDN论坛上的提示更改了app.config(maxconnection应该是12 * CPU核心数):
<system.net>
<settings>
<servicePointManager expect100Continue="false" useNagleAlgorithm="false"/>
</settings>
<connectionManagement>
<add address = "*" maxconnection = "48" />
</connectionManagement>
</system.net>
再次运行应用程序后,我得到以下输出:
Starting sequential inserts.
Adding item 0. Thread ID: 10
Adding item 1. Thread ID: 10
..
Adding item 999. Thread ID: 10
Done in: 00:00:18.9342480
从 3 分多钟到 18 秒不等。多么不同啊!但我们可以做得更好。下面是一些使用分区器插入所有项目的代码(插入将并行发生):
private static void ParallelInserts(CloudTableClient client)
{
Trace.WriteLine("Starting parallel inserts.");
var stopwatch = new Stopwatch();
stopwatch.Start();
var partitioner = Partitioner.Create(0, 1000, 10);
var options = new ParallelOptions { MaxDegreeOfParallelism = 8 };
Parallel.ForEach(partitioner, options, range =>
{
var context = client.GetDataServiceContext();
for (int i = range.Item1; i < range.Item2; i++)
{
Trace.WriteLine(String.Format("Adding item {0}. Thread ID: {1}", i, Thread.CurrentThread.ManagedThreadId));
context.AddObject(TABLENAME, new MyEntity()
{
Date = DateTime.UtcNow,
PartitionKey = "Test",
RowKey = Guid.NewGuid().ToString(),
Text = String.Format("Item {0} - {1}", i, Guid.NewGuid().ToString())
});
context.SaveChanges();
}
});
stopwatch.Stop();
Trace.WriteLine("Done in: " + stopwatch.Elapsed.ToString());
}
结果:
Starting parallel inserts.
Adding item 0. Thread ID: 10
Adding item 10. Thread ID: 18
Adding item 999. Thread ID: 16
..
Done in: 00:00:04.6041978
瞧,我们从 3 分 39 秒下降到 18 秒,现在甚至下降到 4 秒。
关于c# - 如何使用 azure 存储表实现每秒 10 次以上的插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12750302/