查询,首先选择固定主题,然后选择所有其他主题

时间:2010-11-07 23:59:07

标签: sql mysql optimization

我目前的查询是......

SELECT * FROM (
                        SELECT * FROM (
                            SELECT
                                topic.id, topic.title, topic.description, topic.member_id, topic.member_name, topic.views, topic.post_count, topic.last_post_time, topic.last_post_author, topic.last_post_author_id
                            FROM topic
                            INNER JOIN topic_status_assoc
                                ON topic_status_assoc.topic_id = topic.id
                            INNER JOIN topic_status
                                ON topic_status.id = topic_status_assoc.status_id
                            WHERE topic_status.id = 2
                            AND topic.board_id = ".$this->_getId()."
                            AND EXISTS (
                                SELECT
                                    id
                                FROM post
                                WHERE trash = 0
                                    AND topic_id = topic.id
                            )
                        ORDER BY last_post_time DESC
                    ) tab2
                    UNION ALL
                    SELECT * FROM (
                        SELECT
                            topic.id, topic.title, topic.description, topic.member_id, topic.member_name, topic.views, topic.post_count, topic.last_post_time, topic.last_post_author, topic.last_post_author_id
                        FROM topic
                        WHERE board_id = ".$this->_getId()."
                            AND id NOT IN (
                                SELECT
                                    topic.id
                                FROM topic
                                    INNER JOIN topic_status_assoc
                                        ON topic_status_assoc.topic_id = topic.id
                                    INNER JOIN topic_status
                                        ON topic_status.id = topic_status_assoc.status_id
                                WHERE topic_status.id = 2
                                    AND topic.board_id = ".$this->_getId()."
                                    AND EXISTS (
                                        SELECT
                                            id
                                        FROM post
                                        WHERE trash = 0
                                            AND topic_id = topic.id
                                    )
                            )
                            AND EXISTS (
                                SELECT
                                    id
                                FROM post
                                WHERE trash = 0
                                    AND topic_id = topic.id
                            )
                        ORDER BY last_post_time DESC
                    ) tab3
                    WHERE post_count > 0
                ) tab1
                LIMIT ".$start.", ".$count

我相信你会同意我的看法,这看起来效率不高。关于我如何以不同方式做到这一点的任何想法,某种方式疯狂地优化这个查询^ _ ^

1 个答案:

答案 0 :(得分:0)

MySQL是我所知道的唯一一个允许UNION语句中的括号允许独立的ORDER BY(和扩展名,LIMIT)子句的数据库:

 (SELECT t.id, 
         t.title, 
         t.description, 
         t.member_id, 
         t.member_name, 
         t.views, 
         t.post_count, 
         t.last_post_time, 
         t.last_post_author, 
         t.last_post_author_id
    FROM TOPIC t
    JOIN topic_status_assoc tsa ON tsa.topic_id = t.id
    JOIN topic_status ts ON ts.id = tsa.status_id
                        AND ts.id = 2
   WHERE t.board_id = ".$this->_getId()."
     AND t.post_count > 0
     AND EXISTS (SELECT NULL
                   FROM POST p
                  WHERE p.trash = 0
                    AND p.topic_id = t.id)
ORDER BY t.last_post_time DESC) 
UNION ALL
(SELECT t.id, 
        t.title, 
        t.description, 
        t.member_id, 
        t.member_name, 
        t.views, 
        t.post_count, 
        t.last_post_time, 
        t.last_post_author, 
        t.last_post_author_id
   FROM TOPIC t
  WHERE t.board_id = ".$this->_getId()."
    AND t.post_count > 0
    AND NOT EXISTS (SELECT NULL
                      FROM TOPIC
                      JOIN topic_status_assoc ON topic_status_assoc.topic_id = topic.id
                      JOIN topic_status ON topic_status.id = topic_status_assoc.status_id
                                       AND topic_status.id = 2
                     WHERE topic.board_id = ".$this->_getId()."
                       AND EXISTS (SELECT NULL
                                    FROM POST p
                                    WHERE p.trash = 0
                                      AND p.topic_id = topic.id))                    
               AND EXISTS (SELECT NULL
                             FROM POST p
                            WHERE p.trash = 0
                              AND p.topic_id = t.id)
                    ORDER BY last_post_time DESC) 
 LIMIT ".$start.", ".$count

请花点时间了解使用表别名。