如何使用JOIN或其他操作优化此mySQL查询?

时间:2014-12-16 08:53:00

标签: mysql optimization

请原谅新手问题,但我对mySQL仍然相当新......只是让事情变得棘手。我只花了几个小时写下这个怪物:

SELECT orders_comments.orderID, orders_comments.mediaID, orders_comments.imageURL, orders_comments_comments.comment
    FROM orders_comments 
    JOIN orders_comments_comments 
    ON orders_comments.orderID = orders_comments_comments.orderID
    JOIN 
    (
        SELECT DISTINCT orderID
        FROM 
        (
            SELECT * FROM orders_comments
            WHERE orders_comments.orderID NOT IN 
            ( 
                SELECT log_comments.orderID 
                FROM log_comments 
                JOIN users_comments 
                ON (log_comments.userID = users_comments.userID) 
                WHERE users_comments.userID = :userID 
            )
            AND orders_comments.userID != :userID 
        ) validOrders
        ORDER BY orderID ASC 
        LIMIT :numOrdersToReceive
    ) distinctOrders
    ON orders_comments.orderID = distinctOrders.orderID

我已经读过,子查询通常不好,你可以用JOIN操作做得更好,但是这个查询的复杂性让我心烦意乱。快速浏览一下,有没有明显的改进?

0 个答案:

没有答案