所以我有一個查詢: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