如何加速codeigniter / php分页,sql-query

时间:2013-09-23 13:33:39

标签: php mysql sql codeigniter sqlperformance

my sql tables 编辑: usr_id,cat_ids,有效,id_1,id_2,id_3 =>索引。当然所有id都是主键 这是我的SQL表,以及我使用codeigniter进行的查询:

function getAllPosts($limit = 0, $start = 0)
{
    $data = $this->db->select('p.*, u.nickname, u.usr_status, u.usr_rating, pc.*, c1.category as category_1, c2.category as category_2, c3.category as category_3')
                 ->from('posts p')
                 ->join('users u', 'p.usr_id = u.id', 'inner')
                 ->join('post_categories pc', 'p.cat_ids = pc.id', 'inner')
                 ->join('categories c1', 'pc.id_1 = c1.id', 'inner')
                 ->join('categories c2', 'pc.id_2 = c2.id', 'left')
                 ->join('categories c3', 'pc.id_3 = c3.id', 'left')
                 ->limit($limit, $start)
                 ->order_by('p.id', 'desc')
                 ->where('p.active', 1)
                 ->get()
                 ->result_array();

还有控制器:

    $config['base_url']         = base_url() . 'posts/'. $base_url;
    $config['total_rows']       = $this->db->count_all('posts');
    $config['per_page']         = 10;
    $config['num_links']        = 5;
    $config['use_page_numbers'] = TRUE;
    $config['uri_segment']      = 3;
    $this->pagination->initialize($config);
    $page = $this->uri->segment(3);
    $data['posts'] = $this->m_posts->getAllPosts($config['per_page'], $page);
    $data['pagination'] = $this->pagination->create_links();

那么我怎样才能加快所有这些分页的内容......因为当有1.5k页(15000个帖子)时,它会慢下来......  我想我可以提供所有信息,但我可以自由地要求更多信息

1 个答案:

答案 0 :(得分:0)

您可以通过从表中运行解释查询来获得更具体的优化提示。

http://dev.mysql.com/doc/refman/5.0/en/using-explain.html

它标识了您可以在哪里添加索引(以及索引的kinf)以更好地进行性能调整。

更多普通信息可以在这里找到:http://www.sitepoint.com/using-explain-to-write-better-mysql-queries/

希望它有所帮助!