Entity Framework Extensions Batch Size

Description

Batch size is the number of records in each batch. The rows in a batch are sent to the server at the end of each batch.

The BatchSize property gets or sets the number of records to use in a batch. The following example save bulk data in batches of 1000 rows.

context.BulkSaveChanges(options => options.BatchSize = 1000);

Try it: EF Core | EF6

  • A batch is complete when BatchSize rows have been processed or there are no more rows to send to the destination data source.
  • The default value is zero, which indicates a default by size depending on the provider. For example, 10 000 for SQL Server.

You can also set batch size globally using EntityFrameworkManager.BulkOperationBuilder.

// Global
EntityFrameworkManager.BulkOperationBuilder = builder => builder.BatchSize = 1000;

Purpose

Having access to modify the BatchSize default value may be useful in some occasions where the performance is very affected.

Don't try to optimize it if your application is not affected by the performance problem.

FAQ

What's the optimized BatchSize?

Not too low, not too high!

Unfortunately, there is no magic value.

  • If you set it too low, the library will perform too many round-trips and it may decrease the overall performance.
  • If you set it too high, the library will make fewer round-trips, but it could decrease the overall performance by taking more time to write on the server.

There is no perfect number since there are too many factors such as:

  • Column Size
  • Index
  • Latency
  • Trigger
  • Etc.

Last updated: 2023-03-01
Author:


Contents