有没有办法更快地运行PHP长循环代码?

时间:2016-12-07 08:28:49

标签: php codeigniter

我有一个代码,它在外部(慢速)API上运行查询,并循环遍历大量变量并插入数据,发送电子邮件,等等。 这是主要功能作为一个例子:

// MAIN: Cron Job Function
public function kas_alert() {


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

    // 1. Get 'prod' table
    $data['table'] = $this->kas_model->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->kas_model->get_last_row_of_tag($row->tag_id);
        $last_row       = $last_row_query[0];
        $l_aaa_id       = $last_row->prod_aaa_id;
        $l_and_id       = $last_row->prod_bbb_id;
        $l_r_aaa        = $last_row->dat_data1_aaa;
        $l_r_and        = $last_row->dat_data1_bbb;
        $l_t_aaa        = $last_row->dat_data2_aaa;
        $l_t_and        = $last_row->dat_data2_bbb;
        $tagword        = $last_row->tag_word;
        $tag_id         = $last_row->tag_id;
        $country        = $last_row->kay_country;
        $email          = $last_row->u_email;
        $prod_name      = $last_row->prod_name;
        // For the Weekly report:
        $prod_id        = $last_row->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->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->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->kas_model->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 "<h1>prod Name: $prod_id</h1>";
        echo "<h2>tag id: $tag_id</h2>";
        var_dump($aaa_data_today);
        echo "aaa old: ";
        echo $l_r_aaa;
        echo "<br> aaa new: ";
        echo $aaa_data_today['data1']; 

        var_dump($bbb_data_today);
        echo "<br> bbb old: ";
        echo $l_r_and;
        echo "<br> bbb new: ";
        echo $bbb_data_today['data1'];

        // 2.5. Check if there is a need to send something
        $send = $this->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->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);
        }

    }


}

这个CodeIgniter控制器每天都在运行,我使用CPU和RAM几乎没有任何东西运行(Cronjob)太长时间(RAM为400M / 4.25G,CPU仅为0.7%-1.3%)。

我想知道是否有一个选项可以将所有foreach循环拆分为较小的线程(并且我不确定是否会在这里执行分支)并且并行运行所有foreach循环但是它不会让我的服务器崩溃。

我不是DevOps而且非常感兴趣的学习 - 在这种情况下我该怎么办?

0 个答案:

没有答案