2010-11-07 3 views
0

Ma requête en cours est ...requête, sélectionnez épinglées sujets d'abord, puis tous les autres sujets

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 

Je suis sûr que vous seriez d'accord avec moi que cela ne fonctionne tout simplement pas aussi efficace qu'il pourrait être. Toutes les idées sur comment je pourrais le faire différemment, une façon d'optimiser cette requête^_^

Répondre

0

MySQL est la seule base de données que je connaisse qui permet aux crochets dans les instructions UNION d'autoriser ORDER BY (et par extension , LIMIT) clauses:

(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 

Veuillez prendre le temps d'apprendre à utiliser les alias de table.

+0

J'ai toujours évité les alias de table pour faciliter la lecture du code. Cela a-t-il un effet sur les performances de MySQL? – Webnet

Questions connexes