SQL优化之用union all取代in或or

使用or:

WHERE * FROM article
WHERE article_category=2
OR article_category=3
ORDER BY article_id DESC
LIMIT 5
// 执行时间:11.0777

使用in:

SELECT * FROM article
WHERE article_category IN (2,3)
ORDER BY article_id DESC
LIMIT 5
// 执行时间:11.2850

使用union all:

(
    SELECT * FROM article
    WHERE article_category=2
    ORDER BY article_id DESC
    LIMIT 5
) UNION ALL (
    SELECT * FROM article
    WHERE article_category=3
    ORDER BY article_id DESC
    LIMIT 5
)
ORDER BY article_id DESC
LIMIT 5
// 执行时间:0.0261

原文地址:http://laiguowei2004.blog.163.com/blog/static/368290002011716111921116/