如何提高php的插入速度?

niwlg2el  于 2021-06-19  发布在  Mysql
关注(0)|答案(1)|浏览(380)

我正在尝试读取电子邮件的文本文件,并尝试将这些电子邮件插入到具有某些修改值的数据库表中。使用cake-php和mysql数据库。这是我的密码:
控制器文件:

public function addLead()
{
    //$this->layout=false;
    //$this->autoRender=false;
    set_time_limit(100000);
    $this->loadModel('Client');
    $user=$this->Session->read("Auth.User");
    if ($this->request->is('post')) {
        $count = 0;
        $upFile = $this->request->data['Lead']['filename'];
        $file_ext = @strtolower(end(explode('.', $upFile['name'])));
        $expensions = array("txt");
        $result = "";
        if (in_array($file_ext, $expensions) === false) {
            $errors[] = "extension not allowed, please choose a txt file.";
        }

        if (empty($errors) == true) {
            $myfile = fopen($upFile['tmp_name'], "r") or die("Unable to open file!");
            while (!feof($myfile)) {
                $lead = fgets($myfile, 4096);
                $lead = preg_replace('/\s+/', '', $lead);
                $pieces = explode("@", $lead);
                $clientMailBox = $pieces[0];
                $clientMailHost = $pieces[1];
                $clientInfo = $this->Client->addClient($user['id'], $clientMailBox, $clientMailBox, $clientMailHost,$lead, "","","","");

            }
            fclose($myfile);
        } else {

        }
        //$this->set('result', $elinkDec);
        $this->Session->setFlash(__("<div class=\"alert alert-danger flash animated\">$count Leads Added<button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">&times;</button></div>"));

    }

}

模型函数:

public function addClient($userId=null, $clientName = null, $clientMailBox = null, $clientMailHost = null,$clientEmail=null, $emailFrom = null,$msgId=null,$msgSubject=null,$pho=null)
{
    $getClient = $this->find('first',array('conditions'=>array('user_id'=>$userId,'email'=>$clientEmail,'status'=>1)));
    if (empty($getClient)) {
        $this->set(array('id' => null,'user_id'=>$userId, 'email_from' => $emailFrom,'phone'=>$pho, 'name' => $clientName, 'mailbox' => $clientMailBox, 'host' => $clientMailHost, 'email' => $clientEmail,'email_from'=>$emailFrom,'message_id'=>$msgId,'subject'=>$msgSubject,'response'=>1, 'status' => 1,));
        $saveClient = $this->save();
        return $saveClient;
    } else {
        if($getClient['Client']['response_status']==1){
            if($pho != '0') $this->set(array('id' => $getClient['Client']['id'],'phone'=>$pho,'response'=>$getClient['Client']['response']+1, 'message_id' => $msgId,'subject' => $msgSubject,  'response_status' => 0,));
            else $this->set(array('id' => $getClient['Client']['id'],'response'=>$getClient['Client']['response']+1, 'message_id' => $msgId,'subject' => $msgSubject,  'response_status' => 0,));
            $saveClient = $this->save();
            return $saveClient;
        }
    }
}

但它的速度太慢了。。。。我正在尝试插入10万封电子邮件,它需要超过30分钟执行。

tktrz96b

tktrz96b1#

可能是10万个发现(第一,…)正在放慢速度。你试过把递归属性设置为-1吗(e、 g$this->client->recursive=-1;)或者尝试将find(首先,conditions)更改为hasany(conditions)(hasany使用find('count'),使用与find中设置的条件相同的条件,但同时设置递归属性。

相关问题