本文介绍了连续几个SubmitChanges非常慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个问题:为什么当我使用以下代码

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

大约8秒钟后结束

但是当我使用此代码时

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

大约50秒后结束

我该如何做第二个例子,因为它对我来说更合适?

解决方案

每次调用SubmitChanges时,Linq-to-Sql都会检查每个对象的跟踪更改,因此将更改更改为1个对象,然后迭代相同的更改10000次并调用SubmitChanges将成倍增加Linq-to-Sql处理更改所需的时间,因为每次都添加一个额外的项.

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

but 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.

这篇关于连续几个SubmitChanges非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 18:06