2012-12-28 97 views
0

優化PyroCMS代碼,我建立了這個方法,下面從博客模塊爲關鍵詞

public function genres($genre = null) 
{ 
    $this->db->order_by('name', 'ASC'); 
    $result = $this->db->get('keywords'); 
    $genres = $result->result(); 

    if($genre) 
    { 
     $this->load->model('genres_m'); 
     // decode encoded cyrillic characters 
     $genre = rawurldecode($genre) OR redirect('generos'); 

     $time[] = time(); 
     // Count total blog posts and work out how many pages exist 
     $pagination = create_pagination(lang('ebooks:routes:genres') . '/' . $genre, $this->genres_m->count_genres_by($genre, array('entry_active' => 1)), NULL, 4); 

     $time[] = time(); 
     // Get the current page of blog posts 
     $books = $this->genres_m 
      ->limit($pagination['per_page']) 
      ->order_by('info_title', 'ASC') 
      ->get_genres_by($genre, array('entry_active' => 1)); 

     $time[] = time(); 
     foreach ($books AS &$book) 
     { 
      $book->books_info_genre = Keywords::get($book->books_info_genre, 'blog/tagged'); 
      $book->url = site_url(lang('ebooks:routes:ebook') . '/' . $book->info_title . '/' . $book->id); 
     } 

     $time[] = time(); 
     // Set meta description based on post titles 
     //$meta = $this->_posts_metadata($books); 

     $name = str_replace('-', ' ', $genre); 

     // Build the page 
     $this->template 
      ->title($this->_template_title(lang('ebooks:of').' '.$name)) 
      ->set_metadata('description', $this->_template_title(lang('ebooks:of').' '.$name)) 
      ->set_metadata('keywords', $this->_template_title(lang('ebooks:of').' '.$name)) 
      ->set('genres', $genres) 
      ->set('books', $books) 
      ->set('genre', $genre) 
      ->set('time', $time) 
      ->set('pagination', $pagination) 
      ->build('genres-list'); 
    } 
    else 
    { 
     $this->template->title($this->_template_title(lang('ebooks:genres_by'))) 
      ->set_metadata('description', $this->_template_title(lang('ebooks:genres_by'))) 
      ->set_metadata('keywords', $this->_template_title(lang('ebooks:genres_by'))) 
      ->set('genres', $genres) 
      ->build('genres-list'); 
    } 
} 

而被標記()方法,這是模型:

public function count_genres_by($genre, $params) 
{ 
    return $this->db->select('*') 
     ->from('downloads_books_book_info') 
     ->join('keywords_applied', 'keywords_applied.hash = downloads_books_book_info.books_info_genre') 
     ->join('keywords', 'keywords.id = keywords_applied.keyword_id') 
     ->where('keywords.name', str_replace('-', ' ', $genre)) 
     ->where($params) 
     ->count_all_results(); 
} 

    public function get_genres_by($genre, $params) 
{ 
    return $this->db->select('*') 
     ->from('downloads_books_book_info') 
     ->join('keywords_applied', 'keywords_applied.hash = downloads_books_book_info.books_info_genre') 
     ->join('keywords', 'keywords.id = keywords_applied.keyword_id') 
     ->where('keywords.name', str_replace('-', ' ', $genre)) 
     ->where($params) 
     ->get() 
     ->result(); 
} 

正如你可以在第一次看到部分代碼,我有時間()四次,給予延遲:

18:49 - 19:03 - 19:41 - 19:41

我有一個約5K條目的數據庫。我如何優化這段代碼?

回答

0

您可以使用2.2.0-beta1並利用搜索系統,該系統將所有關鍵字存儲爲文本,併爲您的查詢刪除一些連接。

否則,您可以使用博客事件構建自己的索引表,這會將關鍵字存儲在博客ID的旁邊。

主要問題是您正在每個返回的單個項目上運行SQL查詢,而一次性計算出所有關鍵字會更快。即使是一個SQL子查詢也會稍微快一點。

+0

謝謝。我必須嘗試新的搜索系統。 –