我有這個查詢我設計的應用程序。有一個引用表,一個作者表和一個reference_authors表。有一個子查詢返回給定參考的所有作者,然後顯示格式在PHP中。子查詢和查詢單獨運行都很好,並且很快。然而,只要子查詢被放入主查詢中,整個事情就會花費超過120秒的時間才能運行。我會在這一個上看到一些新鮮的眼睛。 謝謝。幫我優化這個查詢
SELECT
rf.reference_id,
rf.reference_type_id,
rf.article_title,
rf.publication,
rf.annotation,
rf.publication_year,
(SELECT GROUP_CONCAT(a.author_name)
FROM authors_final AS a
INNER JOIN reference_authors AS ra2 ON ra2.author_id = a.author_id
WHERE ra2.reference_id = rf.reference_id
GROUP BY ra2.reference_id) AS authors
FROM
references_final AS rf
INNER JOIN reference_authors AS ra ON rf.reference_id = ra.reference_id
LEFT JOIN reference_institutes AS ri ON rf.reference_id = ri.reference_id;
這裏是固定查詢。感謝球員的建議。
SELECT
rf.reference_id,
rf.reference_type_id,
rf.article_title,
rf.publication,
rf.annotation,
rf.publication_year,
GROUP_CONCAT(a.author_name) AS authors
FROM
references_final as rf
INNER JOIN (reference_authors AS ra INNER JOIN authors_final AS a ON ra.author_id = a.author_id)
ON rf.reference_id = ra.reference_id
LEFT JOIN reference_institutes AS ri ON rf.reference_id = ri.reference_id
GROUP BY rf.reference_id
您是否嘗試將子查詢重寫爲內連接? –
我知道我錯過了一些東西,我知道。只要看看你推薦的catcall,並將子查詢重寫爲內連接,查詢現在需要0.078秒。感謝百萬隊友。 – jiraiya