photocode photocode - 5 days ago 4x
MySQL Question

Running PHP & Mysqli queries in Parallel

I'm tying to extract data from thousands of premade sql files. I have a script that does what I need using the Mysqli driver in PHP, but it's really slow since it's one sql file at a time. I modified the script to create unique temp database names, which each sql file is loaded into. Data is extracted to an archive database table, then the temp database is dumped. In an effort to speed things up, I created a script structured 4 scripts similar to the one below, where each for loop is stored in it's own unique PHP file (the code below is only for a quick demo of what's going on in 4 separate files), they are setup to grab only 1/4 of the files from the source file folder. All of this works perfectly, the scripts run, there is zero interference with file handling. The issue is that I seem to get almost zero performance boost. Maybe 10 seconds faster :( I quickly refreshed my PHPmyadmin database listing page and could see the 4 different databases loaded at anytime, but I also noticed that it looked like it was still running more or less sequentially as the DB names were changing on the fly. I went the extra step of creating an unique user for each script with it's own connection. No improvement. Can I get this to work with mysqli / PHP or do I need to look into some other options? I'd prefer to do this all in PHP if I can (version 7.0). I tested by running the PHP scripts in my browser. Is that the issue? I haven't written any code to execute them on the command line and set them to the background yet. One last note, all the users in my mysql database have no limits on connections, etc.

$numbers = array('0','1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20');

$numCount = count($numbers);
$a = '0';
$b = '1';
$c = '2';
$d = '3';

$rebuild = array();


for($a; $a <= $numCount; $a+=4){

if(array_key_exists($a, $numbers)){
echo $numbers[$a]."<br>";


echo "<br>";

for($b; $b <= $numCount; $b+=4){

if(array_key_exists($b, $numbers)){
echo $numbers[$b]."<br>";

echo "<br>";

for($c; $c <= $numCount; $c+=4){

if(array_key_exists($c, $numbers)){
echo $numbers[$c]."<br>";


echo "<br>";

for($d; $d <= $numCount; $d+=4){

if(array_key_exists($d, $numbers)){
echo $numbers[$d]."<br>";



Try this:

    class BackgroundTask extends Thread {
        public $output;
        protected $input;

        public function run() {
            /* Processing here, use $output for... well... outputting data */

            // Here you would implement your for() loops, for example, using $this->input as their data

            // Some dumb value to demonstrate
            $output = "SOME DATA!";

        function __construct($input_data) {
            $this->input = $input_data;

    // Create instances with different input data
    // Each "quarter" will be a quarter of your data, as you're trying to do right now
    $job1 = new BackgroundTask($first_quarter);

    $job2 = new BackgroundTask($second_quarter);

    $job3 = new BackgroundTask($third_quarter);

    $job4 = new BackgroundTask($fourth_quarter);

    // ==================

    // "join" the first job, i.e. "wait until it's finished"
    echo "First output: " . $job1->output;

    echo "Second output: " . $job2->output;

    echo "Third output: " . $job3->output;

    echo "Fourth output: " . $job4->output;

When using four calls to your own script through HTTP, you're overloading your connections for no useful reason. Instead, you're taking away spaces for other users who may be trying to access your website.