我尝试使用codeigniter更快地进行搜索,我使用分页库,我必须计算查询返回的记录,该表包含超过1.2百万条记录... num_rows()函数非常慢(大约需要3个小时)
public function search()
{
$this->output->enable_profiler(TRUE);
$data = array();
$query = $this->input->get('query');
$filter = $this->input->get('f');
$hd = $this->input->get('hd');
if($hd == 'true'):
$this->db->where('hd',1);
endif;
$request = urldecode($query);
$where = "MATCH (name,tags) AGAINST ('".$request."' IN BOOLEAN MODE)";
$this->db->where($where);
$get_vars = $this->input->get();
if(is_array($get_vars) && ($this->input->get('query')) ):
$config['suffix'] = '?'.http_build_query($get_vars,'', '&');
endif;
$config['base_url'] = base_url('search');
$config['per_page'] = 8;
$config['num_links'] = 8;
$config['full_tag_open'] = '<div class="pagination"><ul>';
$config['full_tag_close'] = '</ul></div><!--pagination-->';
$config['first_link'] = '« First';
$config['first_tag_open'] = '<li class="prev page">';
$config['first_tag_close'] = '</li>';
$config['last_link'] = 'Last »';
$config['last_tag_open'] = '<li class="next page">';
$config['last_tag_close'] = '</li>';
$config['next_link'] = 'Suivant →';
$config['next_tag_open'] = '<li class="next page">';
$config['next_tag_close'] = '</li>';
$config['prev_link'] = '← Précédent';
$config['prev_tag_open'] = '<li class="prev page">';
$config['prev_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class="active"><a href="">';
$config['cur_tag_close'] = '</a></li>';
$config['num_tag_open'] = '<li class="page">';
$config['num_tag_close'] = '</li>';
$query = clone $this->db;
$config['total_rows'] = $query->get('videos')->num_rows();
$config['segment'] = $this->uri->segment(2);
$this->pagination->initialize($config);
$data['results'] = $this->db->get('videos',$config['per_page'],$this->uri->segment(2))->result();
$this->load->view('search',$data);
}
任何解决方案?
答案 0 :(得分:1)
正如@MonkeyZeus的评论所表明的那样,使用SQL count()
函数可以提高性能。 Codeigniter通过查询构建器函数count_all_results($table = '', $reset = TRUE)
提供此功能。
该功能将考虑您已放置的任何限制器,例如where
,or_where
,like
等
更改此
$query = clone $this->db;
$config['total_rows'] = $query->get('videos')->num_rows();
到这个
//next line is not needed because the query will not be reset by the row count
//$query = clone $this->db;
$config['total_rows'] = $this->db->count_all_results('videos', FALSE);
我有兴趣了解执行时间的变化。