Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to increase insert speed using Bulk insert using AddRange and then SaveChanges in Entity Framework

I have used Entity Framework to insert data into SQL tables.

For larger number of records, instead of Add(), I have used AddRange() and called SaveChanges() later.

It's still taking too much time to insert records - are there any solutions to increase the speed?

_Repository.InsertMultiple(deviceDataList);

await _Repository.SaveAsync();

public void InsertMultiple(List<string> deviceDataList)
{
    context.Devices.AddRange(devices);
}
like image 905
Neo Avatar asked Jul 27 '26 07:07

Neo


1 Answers

Using AddRange over Add is already a great improvement. It fixes the part that's slow in the Application.

However, the SaveChanges still take a lot of time because one database round-trip is made for every entity you save. So if you have 10k entities to insert, 10,000 database round-trip will be made which is INSANELY slow.


Disclaimer: I'm the owner of Entity Framework Extensions

This library is not free but allows you to perform all bulk operations including BulkSaveChanges and BulkInsert:

  • Bulk SaveChanges
  • Bulk Insert
  • Bulk Delete
  • Bulk Update
  • Bulk Merge

Example

// Easy to use
context.BulkSaveChanges();

// Easy to customize
context.BulkSaveChanges(bulk => bulk.BatchSize = 100);

// Perform Bulk Operations
context.BulkDelete(customers);
context.BulkInsert(customers);
context.BulkUpdate(customers);

// Customize Bulk Operations
context.BulkInsert(customers, options => {
   options => options.IncludeGraph = true;
});
context.BulkMerge(customers, options => {
   options.ColumnPrimaryKeyExpression = 
        customer => customer.Code;
});
like image 165
Jonathan Magnan Avatar answered Jul 29 '26 20:07

Jonathan Magnan