0
今天早上我寫了一個快速客戶端,當我注意到後續保存後,性能會降低,只是將一堆數據插入到表存儲中。爲什麼性能會隨着對TableServiceContext.SaveChanges的多次調用而降低
public class MyObject : TableServiceEntity
{
public MyObject()
{
this.RowKey = Guid.NewGuid().ToString();
}
public string SomeProperty { get; set; }
}
然後,我有一個簡單的代碼塊添加一些數據....
Stopwatch timer = new Stopwatch();
for (int i = 0; i < target/50; i++)
{
CloudTableClient client = account.CreateCloudTableClient();
client.CreateTableIfNotExist(entitySet);
TableServiceContext context = client.GetDataServiceContext();
timer.Reset();
timer.Start();
for (int x = 0; x < i * 50; x++)
{
var obj = new MyObject();
context.AddObject(entitySet, obj);
context.SaveChanges();
}
total += 100;
timer.Stop();
Console.WriteLine("Added 100 entities in {0} seconds; total: {1}", timer.Elapsed.Seconds, total);
}
這裏是我所看到的,當它運行(控制檯應用程序)
Added 100 entities in 0 seconds; total: 100
Added 100 entities in 0 seconds; total: 200
Added 100 entities in 1 seconds; total: 300
Added 100 entities in 2 seconds; total: 400
Added 100 entities in 4 seconds; total: 500
Added 100 entities in 4 seconds; total: 600
Added 100 entities in 6 seconds; total: 700
Added 100 entities in 6 seconds; total: 800
爲什麼性能下降?
- 如果我移動背景下,客戶端和/或賬戶圈外
- 實施context.ResolveType並沒有解決問題
- 分析之後,在沒有發生變化, context.SaveChanges方法是瓶頸的地方
- 重新運行該應用程序會重現相同的結果;即使數據庫中有數百/數千個其他實體。
謝謝!它驚人的容易忽略它! – 2012-07-26 18:38:08