dongshanya2008 2010-09-29 16:03
浏览 21
已采纳

如何减少获得此结果所需的查询数

I'm writing an application on top of CodeIgniter to better organize my ebook collection. I'm nearly done, but I realize my 'browse' page is running far too many queries - two per book - to get their information. Obviously not at all ideal, especially since I have about 1000 books to put into this system.

I currently have one model function which gets all of the books (will eventually be modified to take parameters - that's the next step) and another that gets the meta information for each returned book. The second function is the one which makes two queries for each book - one to get the information in the book table and another to get the tags associated with the book. Here are the two model functions:

Get the list of books:

function get_books() {
    $this->db->select('isbn')->order_by('title');
    $query = $this->db->get('books');
    $result = $query->result();
    return $result;
}

Get the book meta information:

function get_book_info($isbn) {
    // Grab the book from Amazon
    $amazon = $this->amazon->get_amazon_item($isbn);

    // Get the book info
    $this->db->select('title, publisher, date, thumb, filename, pages');
    $query = $this->db->get_where('books', array('isbn' => $isbn));
    $bookResult = $query->row();

    // Get the book's tags
    $this->db->select('tag');
    $this->db->from('tags AS t');
    $this->db->join('books_tags AS bt', 'bt.tag_id = t.id', 'left');
    $this->db->where('bt.book_id', $isbn);
    $this->db->order_by('t.tag');
    $tagQuery = $this->db->get();
    foreach ($tagQuery->result() as $row) {
        $tagResult[] = $row->tag;
    }
    $tagResult = implode(', ', $tagResult);

    // Send data
    $data = array(
        'isbn' => $isbn,
        'thumb' => $bookResult->thumb,
        'title' => strip_slashes($bookResult->title),
        'file' => $bookResult->filename,
        'publisher' => strip_slashes($bookResult->publisher),
        'date' => date('F j, Y', strtotime($bookResult->date)),
        'pages' => $bookResult->pages,
        'tags' => $tagResult,
        'rating' => $amazon->Items->Item->CustomerReviews->AverageRating,
        'raters' => $amazon->Items->Item->CustomerReviews->TotalReviews
    );
    return $data;
}

I'm certain there's a way to write one or two queries that will gather all the records into objects I can then filter through, rather than having to write two queries for each one, but I have no idea where to even start trying to write that. Any suggestions are welcome.

Thanks much, Marcus

  • 写回答

5条回答 默认 最新

  • dongtu1789 2010-09-30 01:25
    关注

    With some help from this topic and in others in creating a better query, I was able to resolve this with the following code:

    function get_book_info() {
    
        /*
         * SELECT b.isbn, b.title, b.publisher, b.date, b.thumb, b.filename, b.pages, t.tag
         * FROM books AS b
         * INNER JOIN books_tags AS bt ON b.isbn = bt.book_id
         * INNER JOIN tags AS t ON bt.tag_id = t.id
         * ORDER BY b.title, t.tag
         */
    
        $this->db->select('b.isbn, b.title, b.publisher, b.date, b.thumb, b.filename, b.pages, t.tag');
        $this->db->from('books AS b');
        $this->db->join('books_tags AS bt', 'b.isbn = bt.book_id', 'inner');
        $this->db->join('tags AS t', 'bt.tag_id = t.id', 'inner');
        $this->db->order_by('b.title, t.tag');
        $query = $this->db->get();
        $result = $query->result();
    
        $counter = '';
        $record = $meta = $tags = array();
        $count = count($result);
        $i = 1;
    
        foreach ($result as $book) {
            // If this is not the last row
            if ($i < $count) {
                // If this is the first appearance of this book
                if ($counter != $book->isbn) {
                    // If the meta array already exists
                    if ($meta) {
                        // Add the combined tag string to the meta array
                        $meta['tags'] = implode(', ', $tags);
                        // Add the meta array
                        $record[] = $meta;
                        // Empty the tags array
                        $tags = array();
                    }
                    // Reset the counter
                    $counter = $book->isbn;
                    // Grab the book from Amazon
                    $amazon = $this->amazon->get_amazon_item($book->isbn);
                    // Collect the book information
                    $meta = array(
                        'isbn' => $book->isbn,
                        'title' => strip_slashes($book->title),
                        'publisher' => strip_slashes($book->publisher),
                        'date' => date('F j, Y', strtotime($book->date)),
                        'thumb' => $book->thumb,
                        'file' => $book->filename,
                        'pages' => $book->pages,
                        'rating' => $amazon->Items->Item->CustomerReviews->AverageRating,
                        'raters' => $amazon->Items->Item->CustomerReviews->TotalReviews
                    );
                    // Add the tag to the tags array
                    $tags[] = $book->tag;
                } else {
                    // All we need is the tag
                    $tags[] = $book->tag;
                }
            // If this is the last row
            } else {
                // If this is the first appearance of this book
                if ($counter != $book->isbn) {
                    // Grab the book from Amazon
                    $amazon = $this->amazon->get_amazon_item($book->isbn);
                    // Collect the book information
                    $meta = array(
                        'isbn' => $book->isbn,
                        'title' => strip_slashes($book->title),
                        'publisher' => strip_slashes($book->publisher),
                        'date' => date('F j, Y', strtotime($book->date)),
                        'thumb' => $book->thumb,
                        'file' => $book->filename,
                        'pages' => $book->pages,
                        'rating' => $amazon->Items->Item->CustomerReviews->AverageRating,
                        'raters' => $amazon->Items->Item->CustomerReviews->TotalReviews
                    );
                    // Add the tag to the tags array
                    $tags[] = $book->tag;
                    // Add the combined tag string to the meta array
                    $meta['tags'] = implode(', ', $tags);
                    // Add the meta array
                    $record[] = $meta;
                } else {
                    // All we need is the tag
                    $tags[] = $book->tag;
                    // Add the combined tag string to the meta array
                    $meta['tags'] = implode(', ', $tags);
                    // Add the meta array
                    $record[] = $meta;
                }
            }
            $i++;
        }
    
        return $record;
    }
    

    There may very well be a better way to handle this, but this was how my logic saw it. And only one query, total.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 学习完python基础了,想继续学习该学习什么呢?
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥15 看一下OPENMV原理图有没有错误
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解
  • ¥15 有偿请教计算电磁学的问题涉及到空间中时域UTD和FDTD算法结合的
  • ¥15 vite打包后,页面出现h.createElement is not a function,但本地运行正常
  • ¥15 Java,消息推送配置
  • ¥15 Java计划序号重编制功能,此功能会对所有序号重新排序,排序后不改变前后置关系。