3

I have a requirement to update 20 tables in a SQL Server database in a C# application. For better performance, I am planning to use multiple threads for updating tables. Could anybody refer any example link which gives idea for this kind of operation?

Also, as per my understanding, if I use multi threading, then I have to use different connection object for each thread. In that case, how I can put multiple threads in a single transaction, which are basically using different connection objects?

7
  • Did you try using one stored procedure call which may then update 20 tables. so you would not have to open (even multithreaded) 20 connections? Commented Oct 18, 2013 at 18:44
  • 3
    Are you sure you need multiple threads? You will get better performance only if constructing sql statements takes significant time. If most of the time is spent executing statements then using multiple threads might even make whole process slower. Commented Oct 18, 2013 at 18:46
  • 1
    Why would you need to multithread this? It just feels like a bad idea. 20 tables is nothing. Commented Oct 18, 2013 at 18:47
  • 1
    Write some code to just do the 20 updates first! Don't just jump on multi-threading to "speed" it up before you even know how long it's taking and where the time is going (code vs DB)..... Commented Oct 18, 2013 at 18:49
  • 1
    So if Update "333" (some random update) to Table15 doesn't work, does that not matter to the entire operation? Commented Oct 18, 2013 at 19:05

2 Answers 2

1

Use TPL (task parallel library), here is an example http://safeery2k.wordpress.com/2013/09/17/ado-net-using-tpl/

Sign up to request clarification or add additional context in comments.

Comments

0

20 tables to update at once is pretty crazy.

If 20 stored procedures is the ONLY way to do what needs to get done then create one more stored procedure that will call the 20 stored procedures for you and simply use this stored procedure.

This way if an error/exception occurs you will be able to rollback the mods AND it is now contained in one location - nice and easy (all things considered).

I'm very glad I don't have to deal with the situation you're in with this one! Good luck with this!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.