Batch Options within Entity Framework Extensions
When working with our bulk extensions, performance can often be improved by configuring a few important options.
Here, you'll learn about three key options available in Entity Framework Extensions:
- BatchSize: Number of records per batch. Helps balance speed and resource usage.
- BatchTimeout: Maximum time (in seconds) before a batch operation times out.
- BatchDelayInterval: Delay (in milliseconds) between each batch. Not recommended in a transaction.
These settings help you control how many records are processed at once, how long a batch can run before timing out, and whether there's a delay between batches.
š·ļø BatchSize
The BatchSize option in Entity Framework Extensions defines how many rows are included in each batch. Once the batch is full, it gets sent to the database server.
- A batch is considered "complete" once it reaches the configured size or there are no more rows to send.
- Default value:
0(auto-selected based on the provider and operation type)- SQL Server:
10000 - PostgreSQL:
10000 - MySQL:
200 - MariaDB:
200 - SQLite:
1 - Firebird:
125 - Oracle:
2000for insert/delete,200for update/merge - InMemory:
int.MaxValue - Effort:
int.MaxValue
- SQL Server:
Hereās how to modify the batch size when performing a BulkInsert:
using Z.EntityFramework.Extensions; context.BulkInsert(options => options.BatchSize = 100);
Online Example (EF Core) | Online Example (EF6)
š” What is the recommended BatchSize?
Thereās no one-size-fits-all answerāyou need to test based on your environment.
- Too low? You'll make many round-trips to the server.
- Too high? You'll send fewer requests, but each one could take longer and consume more memory.
What impacts performance:
- Column size
- Indexes
- Server latency
- Triggers
- Network speed
- And more!
You might find that in your development environment, BatchSize = 25000 is optimal, while in production, BatchSize = 2000 works better due to other constraints.
š” Our library is already fast! Donāt over-optimize unless thereās a real need. If everything runs fast enough, thereās usually no reason to change it.
š·ļø BatchTimeout
The BatchTimeout option in Entity Framework Extensions sets how long (in seconds) the library should wait for a batch to complete before timing out.
- Default value: The
context.Database.GetCommandTimeout()value, if one is specified.
A batch timeout error might look like this:
System.Exception: 'A timeout error occurred (Current Timeout = 30). Please increase the timeout globally: ctx.Database.SetCommandTimeout(timeoutValue); or BulkOperationManager.BulkOperationBuilder = operation => operation.BatchTimeout = timeoutValue; or by operation: db.BulkSaveChanges(operation => operation.BatchTimeout = timeoutValue);'
To fix this, you can increase the timeout using Entity Framework:
context.Database.SetCommandTimeout(300);
Or through our library:
Hereās how to set a timeout for a specific BulkInsert:
using Z.EntityFramework.Extensions; context.BulkInsert(options => options.BatchTimeout = 180);
Online Example (EF Core) | Online Example (EF6)
š·ļø BatchDelayInterval
The BatchDelayInterval option in Entity Framework Extensions sets a pause (in milliseconds) between each batch operation.
- Default value:
0(no delay)
Hereās how to set the delay for a BulkInsert:
using Z.EntityFramework.Extensions; context.BulkInsert(list, options => options.BatchDelayInterval = 100);
Online Example (EF Core) | Online Example (EF6)
ā ļø Important: Avoid using this setting inside a transaction. A transaction should be short and fast. Adding a delay:
- Increases the time the transaction stays open
- Raises the risk of locks and deadlocks
- Can negatively affect concurrency and reliability
š Comparison at a Glance
| Option | What it controls | Default Value | Best For |
|---|---|---|---|
| BatchSize | Rows sent per round-trip | Auto-selected per provider | Balancing speed & memory |
| BatchTimeout | Max seconds before timeout | EF context timeout | Handling very large operations |
| BatchDelayInterval | Delay between each batch (ms) | 0 (no delay) | Reducing DB stress in rare cases |
š When to Use
Batch options are useful when you:
- Insert/update millions of rows.
- Hit timeouts with large batches.
- Need to throttle load on the database.
Most of the time, defaults are already tuned for performance ā only tweak these if you hit real issues.
ā Why Itās Useful
With batch options you can:
- Make operations faster by adjusting
BatchSize. - Prevent crashes with
BatchTimeout. - Fine-tune load handling (rarely) with
BatchDelayInterval.
š Related Articles
š Conclusion
Tweaking BatchSize, BatchTimeout, and BatchDelayInterval can help you squeeze more performance out of your bulk operationsābut only when really needed.
- Use
BatchSizeto control how many rows are sent in one go. - Use
BatchTimeoutif you encounter timeout exceptions for large operations. - Use
BatchDelayIntervalonly in very specific scenariosānever inside a transaction.
Most of the time, the default settings are already optimized for typical use cases. Unless you're facing performance issues or working with millions of records, you probably donāt need to change them.
šÆ The goal isnāt to "tweak everything"āit's to fix problems when they show up using the right option.
Author: