What's the fastest way to bulk insert a lot of data in SQL Server (C# client)

前端 未结 8 1142
予麋鹿
予麋鹿 2020-11-28 22:39

I am hitting some performance bottlenecks with my C# client inserting bulk data into a SQL Server 2005 database and I\'m looking for ways in which to speed up the process.

相关标签:
8条回答
  • 2020-11-28 23:27

    My guess is that you'll see a dramatic improvement if you change that index to be nonclustered. This leaves you with two options:

    1. Change the index to nonclustered, and leave it as a heap table, without a clustered index
    2. Change the index to nonclustered, but then add a surrogate key (like "id") and make it an identity, primary key, and clustered index

    Either one will speed up your inserts without noticeably slowing down your reads.

    Think about it this way -- right now, you're telling SQL to do a bulk insert, but then you're asking SQL to reorder the entire table every table you add anything. With a nonclustered index, you'll add the records in whatever order they come in, and then build a separate index indicating their desired order.

    0 讨论(0)
  • 2020-11-28 23:36

    Have you tried using transactions?

    From what you describe, having the server committing 100% of the time to disk, it seems you are sending each row of data in an atomic SQL sentence thus forcing the server to commit (write to disk) every single row.

    If you used transactions instead, the server would only commit once at the end of the transaction.

    For further help: What method are you using for inserting data to the server? Updating a DataTable using a DataAdapter, or executing each sentence using a string?

    0 讨论(0)
提交回复
热议问题