dsfsdf7852 2018-10-18 01:47 采纳率: 100%
浏览 27

使用PHP优化执行时间

I'm processing roughly not less than 25,000 records. However, it somehow exceed the maximum execution time. I am using Codeigniter 3.0.

The Records was the text data from a PDF that was processed by the library I made. It seemed to be not exceeding the execution time if I only show it, but when things starts to be complicated, like processing it to the database(MySQL), it exceeds the 300sec(I reconfigured this) execution time.

To illustrate

  function process() {

        $data = processThePDF(); //outputs the records / 25,000 records

        if ($data) {
                    foreach ($data as $dt) {

                        $info = $this->just_another_model->view($dt['id']); //get the old record

                        if ($info) {
                            //update
                            $this->just_another_model->update([$params]);
                            //Log Update
                            $this->just_another_model->log([$params]);
                        } else {
                            //Register
                            $this->just_another_model->update([$params]);
                            //Log Register
                            $this->just_another_model->log([$params]);
                        }
                    }
                }
}

So my questions are: 1. Is there a better way to optimize this? 2. Is convenient to write a json file or a text file before processing it?

  • 写回答

1条回答 默认 最新

  • dousu8767 2018-10-18 02:30
    关注

    Store your data in an array and update/insert it by batch.

    function process() {
    
    $data = processThePDF(); //outputs the records / 25,000 records
    $update_data = array();
    $update_data_2 = array();
    
    if ($data) {
        foreach ($data as $dt) {
    
            $info = $this->just_another_model->view($dt['id']); //get the old record
    
            if ($info) {        
                $update_data[] = array($params);
            } else {
                $update_data_2[] = array($params);
            }
        }
    
        if(count(update_data) > 0) 
            $this->just_another_model->update_batch($update_data);
        if(count(update_data_2) > 0)
            $this->just_another_model_2->update_batch($update_data_2);
    }
    

    Furthermore, you can get your old records by batch before the loop and format it into array that so you can access it by id: $old_records[$dt['id']]

    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法