【问题标题】:Codeigniter active records query taking too much time to load data from databaseCodeigniter 活动记录查询花费太多时间从数据库加载数据
【发布时间】:2016-08-23 02:36:09
【问题描述】:

我正在使用 codeigniter

这是我的模特

<?php

class groups_Model extends CI_Model {
    public function __construct(){
        parent::__construct();
    }
    /**
     * get All groups
     * @return array object list of groups
     */
    public function get(){
        $this->db->select('groups.id,groups.name as group,status.name as status');
        $this->db->from('groups');
        $this->db->join('status', 'status.id = groups.status');
        $this->db->order_by('groups.id', 'ASC');
        $this->db->limit($this->config->item('per_page'),$this->uri->segment(4));

        $query = $this->db->get();
        $total = $query->num_rows();
        if($total > 0){
            return $query->result();
        }
        return false;
    }

?>  

现在我的数据库中有 500000 条虚拟记录
我正在使用分页获取 20 条记录
但我的查询需要 5 到 6 秒 如何加快数据库性能

控制器代码如下所示

<?php

public function groups(){
    $this->output->enable_profiler(TRUE);
    $this->benchmark->mark('code_start');
    $this->output->cache(5);
    $this->load->model('groups_Model');
    $this->load->library('pagination');
    $this->config->load('pagination');
    $config['base_url'] = base_url().'admin/auth/groups';
    $config['total_rows'] =$this->groups_Model->count(null);
    $this->pagination->initialize($config);
    $data['groupsList']=$this->groups_Model->get();
    $this->load->view('admin/templates/header');
    $this->load->view('admin/groups',$data);
    $this->load->view('admin/templates/footer');
    $this->benchmark->mark('code_end');
    echo $this->benchmark->elapsed_time('code_start', 'code_end');
}
?>  

所有查询都需要这么长时间才能运行

  DATABASE:  database_name (Auth:$db)   QUERIES: 5 (5.8955 seconds)  (Hide)
1.6279      SELECT * FROM groups 
4.2598      SELECT `groups`.`id`, `groups`.`name` as `group`, `status`.`name` as `status`
FROM `groups`
JOIN `status` ON `status`.`id` = `groups`.`status`
ORDER BY `groups`.`id` ASC
 LIMIT 40, 20 
0.0078      SELECT `username`
FROM `users`
WHERE `id` = '1' 
0.0000      SELECT `first_name`
FROM `users`
WHERE `id` = '1' 
0.0000      SELECT `last_name`
FROM `users`
WHERE `id` = '1' 

【问题讨论】:

    标签: php mysql codeigniter


    【解决方案1】:

    使用分页这是最好的使用方式

    创建这些以在模型中发挥作用

    型号

    public function get_count($table){
            return $this->db->count_all_results($table);
        }
    
        public function get_all_userdata($table, $where, $limit, $start){
            $query = $this->db->get_where($table, $where, $limit, $start);
            $data = $query->result_array();
            return $data;
        }
    

    控制器

    $where = array('status' => 0);
             //pagination
            $config['base_url'] = base_url('nonactiveusers');
            $config['total_rows'] =  $this->User_model->get_count();
            $config['per_page'] = 5;
            $config["num_links"] = 3;
            $config['uri_segment'] = 2;
    
            $config['full_tag_open'] = "<ul class='pagination'>";
            $config['full_tag_close'] ="</ul>";
            $config['num_tag_open'] = '<li>';
            $config['num_tag_close'] = '</li>';
            $config['cur_tag_open'] = "<li class='disabled'><li class='active'><a href='#'>";
            $config['cur_tag_close'] = "<span class='sr-only'></span></a></li>";
            $config['next_tag_open'] = "<li>";
            $config['next_tag_close'] = "</li>";
            $config['prev_tag_open'] = "<li>";
            $config['prev_tag_close'] = "</li>";
            $config['first_tag_open'] = "<li>";
            $config['first_tag_close'] = "</li>";
            $config['last_tag_open'] = "<li>";
            $config['last_tag_close'] = "</li>";
    
            $config['first_link'] = "<<";
            $config['last_link'] = ">>";
    
    
            $this->pagination->initialize($config);
            $page = $this->uri->segment(3); // your uri segment here
            $data['links'] = $this->pagination->create_links();
            $result = $this->User_model->get_all_userdata("users", $where, $config['per_page'], $page);
    
            $data['users'] = $result;
            $this->load->view('view', $data);
    

    【讨论】:

    • 我已经在使用分页了,分页在config/pagination.php中
    【解决方案2】:
    $total = $query->num_rows();  
    $total = $this->db->count_all_results($table);
    

    最后一个会给你更快的结果,因为它只返回计数结果。 第一个(你的)比较慢,因为它也返回结果。

    【讨论】:

      猜你喜欢
      • 2018-09-23
      • 1970-01-01
      • 2017-10-03
      • 1970-01-01
      • 1970-01-01
      • 2014-08-19
      • 1970-01-01
      • 2018-01-08
      • 2019-11-14
      相关资源
      最近更新 更多