Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, my requirement is to brake the MySQL database query into parts and execute each part within different threads for example

if user want to see the record from 1 to 1000 then query would be (Select * from record where rec_no > = 1 and rec_no <= 1000). Now instead of running this query I want to run 4 queries such as

1st query= (Select * from Record where rec_no > = 1 and rec_no <= 250)
2nd query = (Select * from Record where rec_no > = 251 and rec_no <= 500)
3rd query =(Select * from Record where rec_no > = 501 and rec_no <= 750)
4th query =Select * from Record where rec_no > = 751 and rec_no <= 1000)

this way all queries would take fraction of second and get executed parallel and fast.
I am getting result from all query but only facing problem to accumulate them in single array which would hold the result and reason behind that any thread would get executed first and return me the result.

part of my code is

C#
private void btnStartThread_Click(object sender, System.EventArgs e)
		{
            Thread[] m_WorkerThread = new Thread[Environment.ProcessorCount];
			

	  // create worker thread instance
            for (int i = 0; i < Environment.ProcessorCount; i++)
            {
                m_WorkerThread[i] = new Thread(WorkerThreadFunction));

                m_WorkerThread[i].Name = "Worker Thread Sample" + i;	

                m_WorkerThread[i].Start("querylist[i]");
            }
		}


C#
private void WorkerThreadFunction( string s)
		{
            //Thread[] t = new Thread[Environment.ProcessorCount];
            
                MyDefinedClass IClass;

                lClass = new MyDefineClass();

                Iclass.Run(s);
            
		}


finally I am getting result in
C#
private void AddString(array[] s)
		{
			
       final_array // would add record one by one, that is result of query 1, then query 2 
                   // till query 4 in order. but I am not aware of thread that is returning me           //result as any thread can return result first                  	
		}


Thanks in advance
Posted
Updated 22-Oct-13 6:39am
v4

1 solution

I see a ton of incorrect assumptions with this...

First, unless each of those threads is creating it's own connection to the database (which is time consuming), your queries, though in separate threads, are being sent and executed one at a time. A MySQL connection will only work on one query at a time.

Second, threads will execute in any order and at any time. You cannot get them to return results in a predetermined order. What you do to compensate for this is to add all the records to your collection, then sort the end result (which is time consuming).

Now, is your MySQL server running on the same machine as your code?? Putting together 4 threads isn't going to do anything at all since the MySQL server is going to be competing for CPU time with, not only your threads, but the hundreds of other threads on the system that are running.

I don't see how you're going to get a performance boost by threading when you have to pay for that boost with more time to assemble and clean up your query.
 
Share this answer
 
Comments
rameshKumar1717 22-Oct-13 13:06pm    
You mean to say that there won't be any improvement in data fetching using multiple thread. Is there any way that my single background thread keep on filling final array and at the same time my UI thread start working with fetched data so that less waiting for him ?
Dave Kreskowiak 22-Oct-13 17:04pm    
More than likely no improvement at all.

What do you mean by "my UI thread start working with fetched data"?? If you're returning a DataSet or DataTable object, you MUST wait for the entire dataset to be returned from the server before you can do anything with it at all.

Now, if you were using a DataReader and getting one record at a time from the query, then you can process data as it's available.

This "UI thread start processing" thing is a bit perplexing as your UI thread shouldn't be doing any processing on the data other than to show it in controls. Even then, you shouldn't be doing something stupid like stuffing a DataGrid with thousands of records. Users don't want to shuffle through that much data and you'll spend a huge amount of time setting up the UI for something that should be really quick.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900