今朝、一連のデータをテーブル ストレージに単純に挿入する簡単なクライアントを作成していたときに、その後の保存後にパフォーマンスが低下することに気付きました。
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 メソッドがボトルネックの場所です
- アプリケーションを再実行すると、同じ結果が再現されます。データベースに数百/数千の他のエンティティがある場合でも。