views:

69

answers:

1

Hi,

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?

+1  A: 

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.

Matthew Abbott