How to get select from SQL Server for multithreading?
I have a C# program that selects all of the rows of a table with about 100 million rows, and performs some analysis on each row. The analysis of each row is independent from that on every other row. I would like to make my app multithreaded in order to speed up computation (I am running on a dual-processor quad-core Intel Xeon). I would like, for two threads, that one thread select the first half (about 50 million) of the rows and that other thread select the second half of the rows. What is the most efficient way to do thi开发者_如何学JAVAs? My rows all have primary ids. My program right now jsut runs select * from table
.
Are you using LINQ and .NET 4.0? If so, you can leverage Parallel Linq to do what you want easily.
Why not just use this?
select * from table where id % 2 = 0
select * from table where id % 2 = 1
I think you need to determine where the bottleneck is. Is your select statement taking 10 seconds to execute, but your code analysis is taking milliseconds? Or is it the other way around?
I would put some metrics together for everything you have going on before attempting to jump head first in to multi-threading. I think you'll find your C# code is going to be very fast and that your SQL is going to slow you down. Look into optimizing your SQL and your database first.
Try running the analysis in the query itself. It might be a lot faster than returning the rows and running the analysis locally.
精彩评论