Entity framework performance issue, saveChanges is very slow



I've been working on several easy EF projects lately. really straightforward,

List<Book> books = entity.Books.WHERE(c=>c.processed==false)ToList();  
    foreach(var book in books)  
      //DoSomelogic, update some properties,   
    book.ISBN = "ISBN " + randomNumber();  
    book.processed = true;  

I putentity.saveChanges inside theforeach Due to the size of the list—about 100k records—if this record processes without issue, flag it and set book. If the process is halted by an exception and processed = true, I won't have to process these good records once more the following time.

I think everything is fine. When processing hundreds of records, it moves quickly. Next, entity when we reach 100,000 records. saveChanges moves along quite slowly. 1-3 seconds for each record. The entity model is then maintained, but replacemententity.saveChanges with vintageSqlHelper.ExecuteNonQuery("update_book", sqlparams) . And it moves quickly.

Could someone please explain why the entity framework processes so slowly? What is the best method to boost performance if I still want to utilize entity.saveChanges?

Many thanks

1/22/2014 2:09:52 AM

Accepted Answer

6/24/2014 8:54:53 PM

Popular Answer

I'd move the SaveChanges(book) command outside of each. You may move this outside and EF will function better with the resulting code because book is on the object as a list.

Since EF is made to optimize updates, creations, and deletions on the back end database, the list is an attribute on the entity. I'm interested if it helps if you do this.

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow