Batching
When you call IUnitOfWork.SaveChanges, LightSpeed flushes all pending changes – inserts, updates and deletes – to the database. To reduce the number of round‑trips to the database, LightSpeed collects these pending changes into batches for submission into the database.
Note that LightSpeed always commits changes under the aegis of a transaction. Therefore, even if there are more SQL statements than will fit in one batch, you are still guaranteed that the save will be atomic.
Customising the Batch Size
By default, LightSpeed collects pending changes into batches of 10.
You can override the default batch size by setting LightSpeedContext.UpdateBatchSize in code, or the updateBatchSize attribute in configuration.
Customising the batch size in configuration |
<add name="Test" |
Customising the batch size in code |
_context.UpdateBatchSize = 20; |
Increasing the batch size will reduce the number of database round‑trips during a large save, but results in much longer SQL commands. Increasing batch size too far can therefore reduce performance instead of improving it. If you make a change, be sure to measure the impact on a system whose configuration (speed, throughput, latency) is as similar as possible to your production system. Particularly in low latency environments, reducing round trips may be a poor trade off.
Identity Columns and Batching
The IdentityColumn identity method defeats batching, because LightSpeed has to query for the database-allocated Id after each insert. Identity columns should be used only when required for compatibility with existing databases.
Databases and Batching
Some databases do not support batching. On these databases, each SQL statement is run as a separate command regardless of batching settings.