Base solution for your next web application
Open Closed

Inserts In Database getting slow in large data #8163


User avatar
0
jur.porras created

I use Hangfire for inserting data into database. The background process has insert query in loops. Notice that starting the hundred records onwards, the performance of inserts are getting slow and decreasing in speed.

Any advise how to optimize the process? I am preparing the code to support a 20 thousands records of inserts.

Thanks


2 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team

    If you use EF Core with Sql server(https://docs.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.infrastructure.relationaloptionsextension.maxbatchsize?view=efcore-3.0) to insert data by default, it is already batch.

    Of course you can use the raw sql statement to batch insert data.

    There are also some third-party free/charged libraries. https://github.com/borisdj/EFCore.BulkExtensions https://entityframework-extensions.net/

  • User Avatar
    0
    mightyit created

    Have you looked at SqlBulkCopy for doing high-performance bulk inserts?

    https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlbulkcopy?view=netframework-4.8