0

我按照 blog 模块中的 tagged() 方法构建了这个方法

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

正如您在代码的第一部分中看到的那样,我得到了 time() 四次,给出了延迟:

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

我有一个大约 5K 条目的数据库。如何优化此代码?

4

1 回答 1

0

您可以使用 2.2.0-beta1 并利用搜索系统,它将所有关键字存储为文本并为您的查询敲出一些连接。

否则,您可以使用博客事件构建自己的索引表,该索引表将存储博客 ID 旁边的关键字。

主要问题是您正在对每个返回的项目运行 SQL 查询,而一次计算所有关键字会更快。即使是 SQL 子查询也会稍微快一些。

于 2013-01-24T17:41:40.853 回答