performance - Slow upload with .csv files and PHP -
i've created function upload csv files using php , insert contents mongo database.
the upload taking time completed , don't have idea how build better code this.
bellow function in php:
public function importemail($file,$list_name,$header){ $data = $this->readcsv($file, $has_header); global $email; $falhas = 0; array_shift($data); if($data === false) { return false; } else { foreach($data $key => $value) { $email->email = $value[$header["email"]]; $email->first_name = $value[$header["fname"]]; $email->last_name = $value[$header["lname"]]; $email->gender = strtoupper($value[$header["gender"]]); $email->status = $value[$header["status"]] !="" ? $value[$header["status"]] : "1"; $email->list_name = $list_name; $email->opt_out = $v = (strtoupper($value[$header["opt"]]) != "" ? strtoupper($value[$header["opt"]]) : "n"); if(!$email->update(trim($value[$header["email"]]))) $falhas++; } } return $falhas === 0 ? true : false; }
edited update() method responsible insert records mongodb
public function update($id) { global $mongo, $gclient; $update = array( '$set' => array( 'userdata.name' => $this->first_name, 'userdata.lastname' => $this->last_name, 'userdata.gender' => $this->gender, 'userdata.optout' => $this->opt_out == "y" || $this->opt_out == "n" ? $this->opt_out : "y", 'userdata.lastupdate' => (int)(microtime(true) * 1000), 'userdata.status' => $this->status == 0 || $this->status == 1 ? +$this->status : 0 ), '$setoninsert' => array( 'userdata.registerdate' => (int)(microtime(true) * 1000) ) ); if(is_string($this->list_name) && strlen($this->list_name) > 0) $update['$push'] = array( 'userdata.meta' => array( 'type' => 'listname', 'name' => $this->list_name ) ); $result = $mongo->{$gclient->slug}->usershistory->update(array( 'useremail' => $id ), $update, array( 'upsert' => true )); if(!$this->email) $this->email = $id; $mongo->{$gclient->slug}->usershistory->update(array( 'useremail' => $id ), array( '$set' => array( 'useremail' => $this->email ) )); return $result['n'] > 0; }
those .csv files bigger 50k (lines).
how can build faster method?
Comments
Post a Comment