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?

User12345
  • 325
  • 1
  • 7
  • 20
  • 1
    Stop using the deprecated `mysql_*` API. Use `mysqli_*` or `PDO` – Jens Jun 19 '15 at 05:17
  • possible duplicate of [Executing functions in parallel](http://stackoverflow.com/questions/9684290/executing-functions-in-parallel) – Pablo Jun 19 '15 at 05:22

1 Answers1

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."]";
        }
    }

}
Helmi Joe
  • 36
  • 2
  • 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? – User12345 Jun 19 '15 at 05:55
  • yes, you can put the query as an array by pre-processing them first inside the loop to get the expected return – Helmi Joe Jun 19 '15 at 06:03
  • 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? – User12345 Jun 19 '15 at 06:15
  • 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 – Helmi Joe Jun 19 '15 at 06:21
  • 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? – User12345 Jun 19 '15 at 07:12
  • btw, what is the query looks like? i think you need to optimize the query :) – Helmi Joe Jun 19 '15 at 07:18
  • Surely, I have to. No doubt about that. I'll give that a go & check back in. Thank you heaps Helmi – User12345 Jun 22 '15 at 03:20