Bulk Insert: How to work with bulk insert operation?
One of the features that is particularly useful when inserting large amount of data is bulk inserting
. This is an optimized time-saving approach with few drawbacks that will be described later.
Syntax
BulkInsertOperation BulkInsert(
string database = null,
BulkInsertOptions options = null);
Parameters | ||
---|---|---|
database | string | Name of database for which bulk operation should be performed. If null then DefaultDatabase from DocumentStore will be used. |
options | BulkInsertOptions | Bulk operations options that should be used. |
Return Value | |
---|---|
BulkInsertOperation | Instance of BulkInsertOperation used for interaction. |
Limitations
There are several limitations to the API:
- Entity Id must be provided by the client. The client by default will use the HiLo generator in order to generate the Id.
- Transactions are per batch, not per operation, and DTC transactions are not supported.
- Documents inserted using bulk-insert will not raise notifications. More about
Changes API
can be found here. - Document Updates and Reference Checking must be explicitly turned on (see
BulkInsertOptions
). AfterCommit
method inPut Triggers
will be not executed in contrast toAllowPut
,AfterPut
andOnPut
.- The default configuration of the bulk insert is not thread safe, ChunkedBulkInsertOptions inside BulkInsertOptions should be set to null in order to support multi threaded usage. Refer to BulkInsertOptions for more details
Example
using (BulkInsertOperation bulkInsert = store.BulkInsert())
{
for (int i = 0; i < 1000 * 1000; i++)
{
bulkInsert.Store(new Employee
{
FirstName = "FirstName #" + i,
LastName = "LastName #" + i
});
}
}