Is there a way to run PHP long loop code faster?
<p>I have a code that runs a query on an external (slow) API and loops through a lot of variables and inserts data, sends email, ect'. 
This is the main function as an example: </p>

<pre><code>// MAIN: Cron Job Function
public function kas_alert() {


    // 0. Deletes all the saved data from the `data` table 1 month+ ago. 
    // $this-&gt;kas_model-&gt;clean_old_rows();

    // 1. Get 'prod' table
    $data['table'] = $this-&gt;kas_model-&gt;prod_table();


    // 2. Go through each row -
    foreach ( $data['table'] as $row ) {

        // 2.2. Gets all vars from the first query.
        $last_row_query = $this-&gt;kas_model-&gt;get_last_row_of_tag($row-&gt;tag_id);
        $last_row       = $last_row_query[0];
        $l_aaa_id       = $last_row-&gt;prod_aaa_id;
        $l_and_id       = $last_row-&gt;prod_bbb_id;
        $l_r_aaa        = $last_row-&gt;dat_data1_aaa;
        $l_r_and        = $last_row-&gt;dat_data1_bbb;
        $l_t_aaa        = $last_row-&gt;dat_data2_aaa;
        $l_t_and        = $last_row-&gt;dat_data2_bbb;
        $tagword        = $last_row-&gt;tag_word;
        $tag_id         = $last_row-&gt;tag_id;
        $country        = $last_row-&gt;kay_country;
        $email          = $last_row-&gt;u_email;
        $prod_name      = $last_row-&gt;prod_name;
        // For the Weekly report:
        $prod_id        = $last_row-&gt;prod_id;
        $today          = date('Y-m-d');

        // 2.3. Run the tagword query again for today on each one of the tags and insert to DB. 
        if ( ($l_aaa_id != 0) || ( !empty($l_aaa_id) ) ) {
            $aaa_data_today   = $this-&gt;get_data1_aaa_by_id_and_kw($l_aaa_id, $tagword, $country);
        } else{
            $aaa_data_today['data1']       = 0;
            $aaa_data_today['data2']      = 0;
            $aaa_data_today['data3'] = 0;
        }

        if ( ($l_and_id != 0) || ( !empty($l_and_id) ) ) {
            $bbb_data_today = $this-&gt;get_data1_bbb_by_id_and_kw($l_and_id, $tagword, $country);
        } else {
            $bbb_data_today['data1']      = 0;
            $bbb_data_today['data2']    = 0;
            $bbb_data_today['data3'] = 0;
        } 

        // 2.4. Insert the new variables to the "data" table. 
        if ($this-&gt;kas_model-&gt;insert_new_tag_to_db( $tag_id, $aaa_data_today['data1'], $bbb_data_today['data1'], $aaa_data_today['data2'], $bbb_data_today['data2'], $aaa_data_today['data3'], $bbb_data_today['data3']) ){
        }


        // Kas Alert Outputs ($SEND is echoed in it's original function)
        echo "&lt;h1&gt;prod Name: $prod_id&lt;/h1&gt;";
        echo "&lt;h2&gt;tag id: $tag_id&lt;/h2&gt;";
        var_dump($aaa_data_today);
        echo "aaa old: ";
        echo $l_r_aaa;
        echo "&lt;br&gt; aaa new: ";
        echo $aaa_data_today['data1']; 

        var_dump($bbb_data_today);
        echo "&lt;br&gt; bbb old: ";
        echo $l_r_and;
        echo "&lt;br&gt; bbb new: ";
        echo $bbb_data_today['data1'];

        // 2.5. Check if there is a need to send something
        $send = $this-&gt;check_if_send($l_aaa_id, $l_and_id, $l_r_aaa, $aaa_data_today['data1'], $l_r_and, $bbb_data_today['data1']);

        // 2.6. If there is a trigger, send the email!
        if ($send) {
            $this-&gt;send_mail($l_aaa_id, $l_and_id, $aaa_data_today['data1'], $bbb_data_today['data1'], $l_r_aaa, $l_r_and, $tagword, $email, $prod_name);
        }

    }


}
</code></pre>

<p>This CodeIgniter controller is runs every day and I get this running (Cronjob) for too long using almost nothing from the CPU and RAM (RAM is at 400M/4.25G and CPU at ONLY 0.7%-1.3%). </p>

<p>I wonder if there's an option to split all foreach loops to smaller threads (and I'm not sure if forking will do here) and run all the foreach loops in parallel but in a way that it wont get my server crashing. </p>

<p>I'm no DevOps and really interested learning - What should I do in this case? </p>