Speed up update of 185k rows in SQL Server 2008?

前端 未结 3 1347
无人及你
无人及你 2020-12-19 14:17

I have a binary file with about 185k rows in it. C# parses file in seconds. What would be the best way to update MSSQL table with

相关标签:
3条回答
  • 2020-12-19 14:44

    Use SqlBulkCopy (to a temporary table) followed by MERGE.

    The bulk-copy method can efficiently transfer data using a "push" from a .NET client. Using BULK INSERT requires a "pull" from the server (along with the required local-file access).

    Then the MERGE command (in SQL Server 2008+) can be used to insert/update/upsert the data from the temporary table to the target table according to the desired rules. Since the data is entirely in the database at this point, this operation will be as fast as can be. Using MERGE may also result in performance advantages over many individual commands, even those within the same transaction.

    See also:

    • Any way to SQLBulkCopy "insert or update if exists"?
    • SQL Server 2008 INSERT Optimization
    0 讨论(0)
  • 2020-12-19 14:58

    I would try table-valued parameter:

    http://www.codeproject.com/Articles/22392/SQL-Server-2008-Table-Valued-Parameters

    0 讨论(0)
  • 2020-12-19 15:01

    Not sure you really want to do it via C#: probably want to use BULK INSERT and give it a file with your data properly formatted.

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