Milligran
Milligran

Reputation: 3161

How to efficiently perform SQL Server database updates from my C# application

I am trying to figure out the best way to design my C# application which utilizes data from a SQL Server backend.

My application periodically has to update 55K rows each one at a time from a loop in my application. Before it does an update it needs to check if the record to be updated exists.

If it exists it updates one field. If not it performs an insert of 4 fields.

The table to be updated has 600K rows.

  1. What is the most efficient way to handle these updates/inserts from my application?
  2. Should I create a data dictionary in c# and load the 600K records and query the dictionary first instead of the database?
  3. Is this a faster approach?
  4. Should I use a stored procedure?
  5. What’s the best way to achieve maximum performance based on this scenario?

Upvotes: 0

Views: 994

Answers (3)

user106701
user106701

Reputation: 175

What if you did something like this, instead?

By some means, get those 55,000 rows of data into the database; if they're not already there. (If you're right now getting those rows from some query, arrange instead for the query-results to be stored in a temporary table on that database. (This might be a proper application for a stored procedure.)

Now, you could express the operations that you need to perform, perhaps, as two separate SQL queries: one to do the updates, and one or more others to do the inserts. The first query might use a clause such as "WHERE FOO IN (SELECT BAR FROM @TEMP_TABLE ...)" to identify the rows to be updated. The others might use "WHERE FOO NOT IN (...)"

This is, to be precise, exactly the sort of thing that I would expect to need to use a stored procedure to do, because, if you think about it, "the SQL server itself" is precisely the right party to be doing the work, because he's the only guy around who already has the data on-hand that you intend to manipulate. He alone doesn't have to "transmit" those 55,000 rows anywhere. Perfect.

Upvotes: 0

bryanmac
bryanmac

Reputation: 39296

You should try to avoid "update 55K rows each one at a time from a loop". That will be very slow.

Instead, try to find a way to batch the updates (n at a time). Look into SQL Server table value parameters as a way to send a set of data to a stored procedure.

Here's an article on updating multiple rows with TVPs: http://www.sqlmag.com/article/sql-server-2008/using-table-valued-parameters-to-update-multiple-rows

Upvotes: 0

Austin Salonen
Austin Salonen

Reputation: 50215

You could use SqlBulkCopy to upload to a temp table then have a SQL Server job do the merge.

Upvotes: 1

Related Questions