Codeigniter active records query taking too much time to load data from database

后端 未结 2 874
悲哀的现实
悲哀的现实 2020-12-12 05:39

I\'m using codeigniter

This is my model



        
相关标签:
2条回答
  • 2020-12-12 06:39
    $total = $query->num_rows();  
    $total = $this->db->count_all_results($table);
    

    The last one will give you faster result, because it only return count result. The first one (yours) is slower because it is also return the result.

    0 讨论(0)
  • 2020-12-12 06:45

    Use pagination this is the best way to use it

    create these to function in model

    Model

    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;
        }
    

    Controller

    $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);
    
    0 讨论(0)
提交回复
热议问题