爲了學習php和codeigniter,我開始創建測試網站...像博客一樣。這不是重點。 我使用codeigniter分頁來顯示我所有博客的帖子。但是......當我的數據庫表(帖子)有超過14K的條目時,它開始變慢......我不需要這樣的回答:「如果它是博客,你永遠不會寫太多帖子,不要考慮那個「......我需要真正的解決方案來加快所有這些東西。如何加快數據庫查詢,當成千上萬的條目
這裏的控制器:
function all()
{
// Pagination $config
$config = $this->m_posts->getPaginationConfig('all', $this->m_posts->getPostsCount());
$this->pagination->initialize($config);
$data['posts'] = $this->m_posts->getAllPosts($config['per_page'], $page);
$data['pagination'] = $this->pagination->create_links();
$this->template->build('posts/posts', $data);
}
型號:
function getPaginationConfig($base_url, $total_rows)
{
$config['base_url'] = base_url() . 'posts/'. $base_url;
$config['total_rows'] = $total_rows;
// ----
$config['per_page'] = 10;
$config['num_links'] = 5;
$config['use_page_numbers'] = TRUE;
$config['uri_segment'] = 3;
return $config;
}
function getPostsCount($limit)
{
$this->db->order_by('id', 'desc');
$q = $this->db->get('posts');
return $q->num_rows();
}
function getAllPosts($limit = 0, $start = 0)
{
$this->db->order_by('id', 'desc');
$this->db->where('active', 1);
// Get LATEST posts -> pagination
$q = $this->db->get('posts', $limit, $start);
$array = $q->result_array();
$data = $this->createPostsArray($array);
return $data;
}
function createPostsArray($array)
{
foreach ($array as $key => $row)
{
$array[$key]['usr_info'] = $this->user->getUserData($row['usr_id'], 'nickname');
$this->load->helper('cat_helper');
$array[$key]['cat_ids'] = explodeCategories($row['cat_ids']);
foreach ($array[$key]['cat_ids'] as $numb => $value)
{
$array[$key]['categories'][$numb] = $this->getCategoryName($value);
}
}
return $array;
}
和您的架構? – Jianhong
那麼我應該在哪裏放這些索引? – user2779986
與14k記錄在一張表中的所有內容?真的嗎? 以下是一些嚴重的解決方案。 索引,可能的歸一化/ denormallization。 :)看看這些方面,我相信你不會失望 –