Multithreaded Search using c#
I want to seach more than 15000 values in a select statement as shown below:
select * from tableA where id in (1,2,3......16000)
Can I use threads, say around 3, and partion 15000 values in diffrent select statement.
select * from tableA where id in (1,2,3......5000)
select * from tableA where id in (5001....10000)
select * from tableA where id in (100开发者_JS百科01....15000)
and run these 3 select statment in parallel.
Yes, but the real question is why?
Something like this might get you started:
var itms = new List<YourDataClass>();
var thr1 = new Thread(new ThreadStart(delegate()
{
// select code
// populate itms
}));
var thr2 = new Thread(new ThreadStart(delegate()
{
// select code
// populate itms
}));
var thr3 = new Thread(new ThreadStart(delegate()
{
// select code
// populate itms
}));
thr1.Start();
thr2.Start();
thr3.Start();
However, that said, if your IDs are integers and (based on your sample) the range of IN
values are sequential, you might want to switch to a where id > 1 and id < 16000
style. This may yeild better performance.
You may tried the parallel programming feature of C# 4.0
It's rather simple:
List<String> jobs = new List<String>();
Parallel.ForEach(jobs, job=>
{
Foo(job);
}
);
Have a look at this: http://msdn.microsoft.com/en-us/library/dd460720.aspx
That is possible and may even be a good idea since sending a (very) large IN
statement to a database may result in errors.
Your database should handle your parallel queries correctly and without problems.
精彩评论