返回查询结果和单个函数中的总行数

时间:2012-06-05 20:53:45

标签: optimization codeigniter count

如果有其他替代方案或是唯一的方法,这是一个可能专注于以最佳方式工作的问题:

使用Codeigniter ...我有列表记录的典型2个功能并显示记录总数(使用页面作为替代)。问题是它们相当大。示例2在我的模型中起作用:

计算行数:

function get_all_count()
    {
        $this->db->select('u.id_user');
        $this->db->from('user u');
        if($this->session->userdata('detail') != '1')
        {
            $this->db->join('management m', 'm.id_user = u.id_user', 'inner');
            $this->db->where('id_detail', $this->session->userdata('detail'));
            if($this->session->userdata('management') === '1')
            {
                $this->db->or_where('detail', 1);
            }
            else
            {
                $this->db->where("id_profile IN (
                                SELECT
                                e2.id_profile
                                FROM profile e, profile e2, profile_path p, profile_path p2
                                WHERE e.id_profile = " . $this->session->userdata('profile') . "
                                AND p2.id_profile = e.id_profile
                                AND p.path LIKE(CONCAT(p2.path,'%'))
                                AND e2.id_profile = p.id_profile
                                )", NULL, FALSE);

                $this->db->where('MD5(u.id_user) <>', $this->session->userdata('id_user'));
            }
        }       
        $this->db->where('u.id_user <>', 1);
        $this->db->where('flag <>', 3);
        $query = $this->db->get();
        return $query->num_rows();  
    }
每页

结果

function get_all($limit, $offset, $sort = '')
    {
        $this->db->select('u.id_user, user, email, flag');
        $this->db->from('user u');
        if($this->session->userdata('detail') != '1')
        {
            $this->db->join('management m', 'm.id_user = u.id_user', 'inner');
            $this->db->where('id_detail', $this->session->userdata('detail'));
            if($this->session->userdata('management') === '1')
            {
                $this->db->or_where('detail', 1);
            }
            else
            {
                $this->db->where("id_profile IN (
                                SELECT
                                e2.id_profile
                                FROM profile e, profile e2, profile_path p, profile_path p2
                                WHERE e.id_profile = " . $this->session->userdata('profile') . "
                                AND p2.id_profile = e.id_profile
                                AND p.path LIKE(CONCAT(p2.path,'%'))
                                AND e2.id_profile = p.id_profile
                                )", NULL, FALSE);

                $this->db->where('MD5(u.id_user) <>', $this->session->userdata('id_user'));
            }
        }
        $this->db->where('u.id_user <>', 1);
        $this->db->where('flag <>', 3);
        if($sort) $this->db->order_by($sort);
        $this->db->limit($limit, $offset);
        $query = $this->db->get();
        return $query->result();    
    }

你看,我重复了大部分功能,区别在于只有字段和管理页面的数量。

我想知道是否有任何替代方法可以在单个函数中获得与查询一样多的结果。我看过很多教程,并且都创建了两个函数:一个用于计数,另一个用于显示结果...是否会有更优化?

1 个答案:

答案 0 :(得分:0)

您必须在查询中使用SQL_CALC_FOUND_ROWS,如下所示:

SELECT SQL_CALC_FOUND_ROWS first_name, age FROM table

要返回结果,您只需创建一个新数组:

$result['total'] = $this->db->query("SELECT FOUND_ROWS() as total")->row_array();
$result['result_array'] = $query->result();
return $result;