0

Lets assume that we are writing a data import app and we will be adding lots of similar entities to the database, let's say 1 million. We create objects in code and then we write them into the database in batches.

Which case is better:

  1. As we create objects, we use dbcontext.Add(object); at the end of the creation.
  2. As we create objects, we add them to a list objectsToSave.Add(object); and after we have 1000 objects in the list, we call dbcontext.AddRange(objectsToSave);

In both cases we call dbcontext.SaveChanges(); after 1000 items are queued in dbcontext to be inserted.

Is there going to be a considerable difference in performance between the two?

Varin
  • 2,354
  • 2
  • 20
  • 37
  • im guessing they are the same. nothing has been allocated to the db yet. you should measure this by yourself first tho. – Daniel A. White Mar 02 '20 at 18:52
  • Actually, you may want to reevaluate whether that's a good way to insert that much data - AFAIK, those particular calls really aren't designed for that. – EJoshuaS - Stand with Ukraine Mar 02 '20 at 18:52
  • 1
    As a point of clarification, why are you loading that much data at once? Is this some kind of initial data load or something? If so, most database systems have a bulk insert function of some kind - you may want to consider doing that instead. – EJoshuaS - Stand with Ukraine Mar 02 '20 at 18:53
  • Does this answer your question? [Entity Framework 6 DbSet AddRange vs IDbSet Add - How Can AddRange be so much faster?](https://stackoverflow.com/questions/43643685/entity-framework-6-dbset-addrange-vs-idbset-add-how-can-addrange-be-so-much-fa) – David Ferenczy Rogožan Mar 02 '20 at 18:54
  • 1
    AddRange is faster but that has to do with entity tracking and not the actual insert. The actual act of inserting would be the same (same produced sql statements and same behavior on execution). – Igor Mar 02 '20 at 18:54
  • Also, you might want to look at [this](https://stackoverflow.com/questions/9075159/how-to-insert-a-data-table-into-sql-server-database-table). It's been awhile since I've done it, but you can use a DataTable to insert large amounts of data into your database. You might get better performance that way. – EJoshuaS - Stand with Ukraine Mar 02 '20 at 18:55
  • Why are you doing through c#? A data import is calling out for an SSIS solution. – JazzmanJim Mar 02 '20 at 18:58
  • From the [documentation](https://learn.microsoft.com/en-us/dotnet/api/system.data.entity.dbset.addrange?view=entity-framework-6.2.0): "_Note that if AutoDetectChangesEnabled is set to true (which is the default), then DetectChanges will be called once before adding any entities and will not be called again. This means that in some situations AddRange may perform significantly better than calling Add multiple times would do._" – David Ferenczy Rogožan Mar 02 '20 at 18:58

0 Answers0