Finding the ideal batch size in Entity Framework

Entity Framework Core inserts entities in batch sizes of 42 (EF Core 5+) but you can change that as shown on the Microsoft docs.

The ideal batch number will be different for every app and will depend on things like latency, entity size, data volume, DB specs, ID generation strategy etc. so you need to try different batch sizes to find the ‘sweet spot‘ for your app.

Here’s some BenchmarkDotNet benchmarks which inserted 1500 records (very simple entity – identity ID) in a local SQL Server and an Azure SQL DB. In this simple example we can see setting the batch size too low or too high affects the timings.

Click the image for a larger few in a new window…

If you’re interested in knowing why the Entity Framework team chose 42 as the default batch size checkout this GitHub issue -> Set a default max batch size for SQL Server · Issue #9270 · dotnet/efcore (github.com)

Leave a Reply

Your email address will not be published.