所以我有一个查询:MySQL查询太慢?
SELECT EWRporta2_articles.*, xf_thread.*, xf_forum.*, xf_user.*, xf_post.message,
IF(NOT ISNULL(xf_user.user_id), xf_user.username, xf_thread.username) AS username
FROM EWRporta2_articles
INNER JOIN xf_thread ON (xf_thread.thread_id = EWRporta2_articles.thread_id)
INNER JOIN xf_forum ON (xf_forum.node_id = xf_thread.node_id)
INNER JOIN xf_post ON (xf_post.post_id = xf_thread.first_post_id)
LEFT JOIN xf_user ON (xf_user.user_id = xf_thread.user_id)
WHERE EWRporta2_articles.article_date < 1417987751
AND xf_thread.discussion_state = 'visible'
ORDER BY EWRporta2_articles.article_date DESC
LIMIT 0, 5
该查询在0.0012秒执行......那好。这个查询所做的是查询文章列表,然后将它们链接到我论坛上的一个主题。
但是,我试图稍微改变查询。虽然上面的查询要求线程存在文章行。我想查找链接到特定文章或存在于特定论坛节点ID中的线索。因此,即使线程中不存在文章行,但如果它具有特定的node_id,它仍会显示出来。这是我对此的查询:
SELECT EWRporta2_articles.*, xf_thread.*, xf_forum.*, xf_user.*, xf_post.message,
IF(EWRporta2_articles.article_date IS NULL, xf_thread.post_date, EWRporta2_articles.article_date) AS article_date,
IF(NOT ISNULL(xf_user.user_id), xf_user.username, xf_thread.username) AS username
FROM xf_thread
LEFT JOIN EWRporta2_articles ON (EWRporta2_articles.thread_id = xf_thread.thread_id)
INNER JOIN xf_forum ON (xf_forum.node_id = xf_thread.node_id)
INNER JOIN xf_post ON (xf_post.post_id = xf_thread.first_post_id)
LEFT JOIN xf_user ON (xf_user.user_id = xf_thread.user_id)
WHERE (xf_thread.node_id IN ('66','78') OR EWRporta2_articles.article_date IS NOT NULL)
AND IF(EWRporta2_articles.article_date IS NULL, xf_thread.post_date, EWRporta2_articles.article_date) < 1417987751
AND xf_thread.discussion_state = 'visible'
ORDER BY article_date DESC
LIMIT 0, 5
此查询的问题是它在0.5683秒内执行。
我能做些什么来提高性能吗?
你没有提供关于你的表的信息,任何存在的索引,也没有提供'EXPLAIN'的结果。没有细节回答这个问题是非常困难的。 – 2014-12-07 21:45:52
除了'explain',''或'可能很难优化。把它写成由'union'连接的两个子查询可能会更好。 – 2014-12-07 21:57:38
只是好奇,是否有一个情况下,user.user_id是NULL,但user.username不是NULL?!?! – Strawberry 2014-12-08 00:17:27