Search code examples
c#sql-serverentity-framework-corebulkinsertsqlbulkcopy

Bulk insert efficiency for inserting a list with one row


I'm attemping to use bulk insert and replace it with current common insertion in my project. Some of insertion requests (BillType.Booklet) are a list with one row and others are a list with multiple row.

   public async Task CreateBill(List<BillReceiverDto> Receivers, BillType BillType)
    {
       
       var bulkList =new List<BillReceiverDto>();

        if (BillType == BillType.Booklet)
        {
            bulkList.Add(Receivers.FirstOrDefault());
        }
        else
        {
            bulkList.AddRange(Receivers);
        }

        await _dbContextProvider.GetDbContext().BulkInsertAsync(bulkList);
    }

Bulk insert have a great performance for inserting huge data, specially more than 100. It insert 5,000 entities in 75 millisecond. But Is it efficient to use bulk insert a list with one row? Is there any drawbacks such as overhead or etc...?


Solution

  • Disclaimer: I'm the owner of Entity Framework Extensions

    It depends on the library you are using Aref Hemati,

    In our library, if there are 10 entities or less to insert, we directly use a SQL statement. So the SqlBulkCopy overhead is not used.

    So using our library even with one row is fine but obviously optimized for hundreds and thousands of rows.