开发者

Several SubmitChanges in succession very slow

开发者 https://www.devze.com 2023-01-02 01:12 出处:网络
One question: Why when I use following code for (int i = 0; i < 10000; i++) { Entity e = new Entity();

One question: Why when I use following code

 for (int i = 0; i < 10000; i++)
  {
      Entity e = new Entity();
      e.DisplayValue = i.ToString();
      ctx.Entities.InsertOnSubmit(e);
  }
  ctx.SubmitChanges();

it finishes after about 8 seconds

开发者_如何学JAVAbut when I use this code

 for (int i = 0; i < 10000; i++)
  {
      Entity e = new Entity();
      e.DisplayValue = i.ToString();
      ctx.Entities.InsertOnSubmit(e);
      ctx.SubmitChanges();
  }

it finishes after about 50 seconds

How can I do faser second example, because it is more situable for me?


Each time you call SubmitChanges, Linq-to-Sql is checking every object for tracked changes, so making the change to 1 object, but then iterating that same change 10000 times and calling SubmitChanges will exponentially increase the time it takes as Linq-to-Sql to process the changes because you are adding an extra item each time.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号