Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework insertion performance

Im testing using Entity Framework with a Azure Sql db. When inserting 1 record, the action takes 400ms. When adding 20 it is 2500ms.

400ms for inserting 1 record via EF seems like a lot.

What is the normal performance rate for EF?

Am I doing something wrong?

Im aware that bulk insertion can be improved, but I thought that a single insert could be done a lot faster!?

var start = DateTime.Now;
testdbEntities testdbEntities = new testdbEntities();

for (int i = 0; i < 20; i++)
    testdbEntities.Users.Add(new User{Name = "New user"});

testdbEntities.SaveChanges();

var end = DateTime.Now;
var timeElapsed = (end - start).TotalMilliseconds;
like image 637
redlaz Avatar asked Dec 19 '25 05:12

redlaz


1 Answers

All common tricks like:

  • AutoDetectChangesEnabled = false
  • Use AddRange over Add
  • Etc.

Will not work like you already have noticed since the performance problem is not within Entity Framework but with SQL Azure

SQL Azure may look pretty cool at first but it's slow as hell unless you paid for a very good Premium Database Tier.

As Evk recommended, you should try to execute a simple SQL Command like "SELECT 1" and you will notice this probably take more than 100ms which is ridiculously slow.

Solution:

  • Move to a better SQL Azure Tier
  • Move away from SQL Azure

Disclaimer: I'm the owner of the project Entity Framework Extensions

Another solution is using this library which will batch multiple queries/bulk operations. However again, even if this library is very fast, you will need a better SQL Azure Tier since it look every database round-trip take more than 200ms in your case.

like image 173
Jonathan Magnan Avatar answered Dec 20 '25 21:12

Jonathan Magnan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!