2

Am looking at executing all DB queries in parallel.

My current code looks as below

mysql_connect("host", "user", "pass");
    $dbcheck = mysql_select_db("db");   

    if ($dbcheck) {

    /* BLOCK - 1*/

        $result_1 = mysql_query($query1);
        if (mysql_num_rows($result_1) > 0) {
            while ($row_1 = mysql_fetch_assoc($result_1)) {
                $a=$row_1["AA1"];
                $b=$row_1["AA2"];
                $a[]="['".$a."',".$b."]";
            }
        }

    /* BLOCK - 2*/

        $result_2 = mysql_query($query2);
        if (mysql_num_rows($result_2) > 0) {
            while ($row_2 = mysql_fetch_assoc($result_2)) {
                $ac1=$row_2["ab1"];
                $ac2=$row_2["ab2"];
                $chart_array_2[]="['".$ac1."',".$ac2."]";
            }
        }
     }

The above runs sequentially I believe. What I'd like to do is execute 'BLOCK - 1' & 'BLOCK - 2' in parallel. I have about 20 such blocks. I'd like to kick them all off in parallel.

I've looked on google. Most of them talk about running DB queries in parallel. I'm not able to figure out a way to implement that with my requirement. Is there a way in PHP that I can kick off each of the blocks in parallel please?

2
  • 1
    Stop using the deprecated mysql_* API. Use mysqli_* or PDO Commented Jun 19, 2015 at 5:17
  • possible duplicate of Executing functions in parallel Commented Jun 19, 2015 at 5:22

1 Answer 1

1

use looping instead

mysql_connect("host", "user", "pass");
$dbcheck = mysql_select_db("db");   

if ($dbcheck) {

for($i=1;$i<=20;$i++) {

    /* BLOCK - $i*/

    $result = mysql_query($query[$i]);
    if (mysql_num_rows($result) > 0) {
        while ($row[$i] = mysql_fetch_assoc($result)) {
            $a[$i]=$row[$i]["AA1"];
            $b[$i]=$row[$i]["AA2"];
            $a[$i][]="['".$a."',".$b."]";
        }
    }

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

7 Comments

Hello Helmi - THankj you very for this. I'll give this a go & update this thread. $result = mysql_query($query[$i]); - Foes this mean I'll have to store my sql queries as an array? As in, $query[0]="bla bla"; $query[1]="bla bla" & so on?
yes, you can put the query as an array by pre-processing them first inside the loop to get the expected return
Hello Helmi - SO, to clarify if I did something on the lines of for($i=1;$i<=20;$i++) { $result[$i] = mysql_query($query[$i]); }, would this kick off the execution of 20 sqls in parallel?
yup, because the loop will loops 20 times,.. you just need additional process inside the loop to adjust the 'AA1' things according to each bloks
I didn't get any errors. However, due to different queries running for different amount of time, the array that captures result set is not able to capture the values :-( Looks like I need java threading kind of thing. By this I mean to say, I need something on the lines of different threads get kicked off in parallel. However the main branch of execution waits until all the threads return & then run through the rest of code. Any idea if this can be done?
|

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.