我有这个查询我设计的应用程序。有一个引用表,一个作者表和一个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