页面加载时间超过一分钟Mysql和Codeigniter

时间:2018-03-30 10:11:34

标签: php mysql codeigniter

我的项目中有页面加载时间问题。 我的表中有近30000条记录并在该表上执行搜索功能,Code工作正常,但页面加载时间非常长(超过一分钟)。在获取结果后,我在ajax中进行了分页,Ajax请求也花费了太多时间(超过分钟)以下是我的代码请给出任何建议来克服这个问题。

 public function get_data($info,$limit,$offset=0){
    if(!is_array($info) && count($info)<1) return false;
    try{
        $orderbyString = "";
        $likestr = "";$or='';$plus='';
        if(isset($info['skill'])){
            foreach($info['skill'] as $key => $val){
                if($key!=0){$or=' OR ';$plus=' + ';}
                $orderbyString .=" ".$plus."  (( CHAR_LENGTH(r.resume) - CHAR_LENGTH( REPLACE ( r.resume, '".strtolower($val)."', '') ) ) / CHAR_LENGTH('".strtolower($val)."')) ";
            }
            $wholeString="ROUND ( ".$orderbyString." ) AS res_count ";
        }
        $this->db->select('r.resume_id, r.jobseeker_id, r.qualification, r.firstname, r.lastname, r.skills as skill_title, r.jobtitle, r.resume_date as last_updated_date, r.jobtype, r.location as city, r.country as state, r.workstatus, r.salary, r.experience, '.$wholeString.' ');
        $this->db->from('jp_resumes as r');
        $this->db->where('r.resume_status','1');

        if(isset($info['time_period']) && $info['time_period']!=='' && count($info['time_period'])>0 ){
            $date = date_create_from_format('m-d-Y', $info['time_period'][0]);
            $start_d=date_format($date, 'Y-m-d');
            $dates = date_create_from_format('m-d-Y', $info['time_period'][1]);
            $end_d=date_format($dates, 'Y-m-d');
            $arr_two=array('r.resume_date >= '=>$start_d,'r.resume_date <= '=>$end_d);
            $this->db->group_start();
            $this->db->where($arr_two);
            $this->db->group_end();
        }
        if(isset($info['skill'])){
            //print_r($info['skill']);exit;
            $this->db->group_start();
            foreach($info['skill'] as $key => $val){
                $arr_two=array('r.skills'=>$val,'r.jobtitle'=>$val,'r.resume'=>$val);   
                $this->db->or_like($arr_two);
            }
            $this->db->group_end();
        }
        if(isset($info['work']) && is_array($info['work']) && count($info['work'])>0 ){
            $this->db->group_start();
            foreach($info['work'] as $key => $val){
                $arr_two=array('r.workstatus'=>$val);    
                $this->db->or_like($arr_two);
            }
            $this->db->group_end();
        }
        if(isset($info['jobtype']) && $info['jobtype']!=='' && count($info['jobtype'])>0 && $info['jobtype'][0] !== ''){
            $this->db->group_start();
            foreach($info['jobtype'] as $key => $val){
                $arr_two=array('r.jobtype'=>$val,'r.emp_type'=>$val);  
                $this->db->or_like($arr_two);
            }
            $this->db->group_end();
        }
        if(isset($info['state']) && count($info['state'])>0 && $info['state']!=''){
            $this->db->group_start();
            foreach($info['state'] as $key=>$val){
                $arr_one=array('r.country'=>$val);
                $this->db->or_like($arr_one);
            }
            $this->db->group_end();
        }
        $this->db->order_by('res_count','desc');
        $this->db->limit($limit,$offset);

        $query = $this->db->get();
        if($query === false){
            log_message('debug',__FUNCTION__." -> ".mysql_error());
        }else{
            if($query->num_rows() > 0)
                return $query->result();
        }
    }catch(Exception $e){
        log_message('error',$e->getMessage());
    }
}

2 个答案:

答案 0 :(得分:0)

我在mysql和codeigniter中遇到了性能问题,我终于发现DNS名称查找超时是问题所在。

我使用的是centos,而我的/etc/resolv.conf(DNS服务器配置)是一些无法访问的IP。 MySQL服务器尝试使用这些IP查找名称并发生超时。

我将skip-name-resolve添加到/etc/my.cnf并重新启动了mysql服务并修复了性能问题。

答案 1 :(得分:0)

问题是使用l​​ike运算符,它需要花费太多时间来执行。我将我的代码从like运算符更新为Match(column_name)反对(值),它的工作正常,现在页面加载时间小于5秒

这是更新的代码

    public function get_resumes_comma($info,$limit,$offset=0){

    if(!is_array($info) && count($info)<1) return false;
    try{
        $orderbyString = "";
        $likestr = "";$or='';$plus='';
        $this->db->select('r.resume_id, r.jobseeker_id, r.qualification, r.firstname, r.lastname, r.skills as skill_title, r.jobtitle, r.resume_date as last_updated_date, r.jobtype, r.location as city, r.country as state, r.workstatus, r.salary, r.experience');
        $this->db->from('jp_resumes as r');
        $this->db->where('r.resume_status','1');

        if(isset($info['time_period']) && $info['time_period']!=='' && count($info['time_period'])>0 ){
            $date = date_create_from_format('m-d-Y', $info['time_period'][0]);
            $start_d=date_format($date, 'Y-m-d');
            $dates = date_create_from_format('m-d-Y', $info['time_period'][1]);
            $end_d=date_format($dates, 'Y-m-d');
            $arr_two=array('r.resume_date >= '=>$start_d,'r.resume_date <= '=>$end_d);
            $this->db->group_start();
            $this->db->where($arr_two);
            $this->db->group_end();
        }
        if(isset($info['skill'])){
            $this->db->group_start();
            foreach($info['skill'] as $key => $val){
                $this->db->or_where('MATCH (resume) AGAINST ("'.$val.'")', NULL, FALSE );
                $this->db->or_where('MATCH (skills) AGAINST ("'.$val.'")', NULL, FALSE );
                $this->db->or_where('MATCH (jobtitle) AGAINST ("'.$val.'")', NULL, FALSE );
                //$this->db->or_like($arr_two);
            }
            $this->db->group_end();
        }
        if(isset($info['work']) && is_array($info['work']) && count($info['work'])>0 ){
            $this->db->group_start();
            foreach($info['work'] as $key => $val){
                $arr_two=array('r.workstatus'=>$val);    
                $this->db->or_like($arr_two);
            }
            $this->db->group_end();
        }
        if(isset($info['jobtype']) && $info['jobtype']!=='' && count($info['jobtype'])>0 && $info['jobtype'][0] !== ''){
            $this->db->group_start();
            foreach($info['jobtype'] as $key => $val){
                $arr_two=array('r.jobtype'=>$val,'r.emp_type'=>$val);  
                $this->db->or_like($arr_two);
            }
            $this->db->group_end();
        }
        if(isset($info['state']) && count($info['state'])>0 && $info['state']!=''){
            $this->db->group_start();
            foreach($info['state'] as $key=>$val){
                $arr_one=array('r.country'=>$val);
                $this->db->or_like($arr_one);
            }
            $this->db->group_end();
        }
        $this->db->limit($limit,$offset);
        $query = $this->db->get();
        if($query === false){
            log_message('debug',__FUNCTION__." -> ".mysql_error());
        }else{
            if($query->num_rows() > 0)
                return $query->result();
        }
    }catch(Exception $e){
        log_message('error',$e->getMessage());
    }
}