php - CodeIgniter 计数太慢 - 分页

标签 php mysql codeigniter pagination

我正在尝试使用 CodeIgniter 更快地进行搜索。我使用分页库,我必须计算从包含超过 120 万条记录的表上的查询返回的记录。 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'] = '&laquo; First';
    $config['first_tag_open'] = '<li class="prev page">';
    $config['first_tag_close'] = '</li>';
    $config['last_link'] = 'Last &raquo;';
    $config['last_tag_open'] = '<li class="next page">';
    $config['last_tag_close'] = '</li>';
    $config['next_link'] = 'Suivant &rarr;';
    $config['next_tag_open'] = '<li class="next page">';
    $config['next_tag_close'] = '</li>';
    $config['prev_link'] = '&larr; 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);
}

有什么解决办法吗?

最佳答案

正如@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); 

我很想知道执行时间是如何变化的。

关于php - CodeIgniter 计数太慢 - 分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39768546/

相关文章:

php - 定期执行 PHP 脚本的最佳方式?

mysql - Sequelize 说 instanceMethod 未定义

python - MySQL 表创建模板 - Python

php - 将对象插入 MySQL 数据库字段

Codeigniter 一笔交易有两种模型

php - .ajax, .text 输入显示函数

php - 如何将 HTML 转换为 BBCode

php - 在一个关键项上使用多个表内连接。不知道出了什么问题

javascript - 如何在 CodeIgniter 中全局包含 jQuery 文件?

php - 谷歌翻译工具包 API 错误 ("Multipart must have Atom and media part")