Note that there are some explanatory texts on larger screens.

plurals
  1. POHelp me optimize this query
    primarykey
    data
    text
    <p>I have this query for an application that I am designing. There is a table of references, an authors table and a reference_authors table. There is a sub query to return all authors for a given reference which I then display formatted in php. The subquery and query run individually are both nice and speedy. However as soon as the subquery is put into the main query the whole thing takes over 120s to run. I would apprecaite some fresh eyes on this one. Thanks.</p> <pre><code>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; </code></pre> <p>Here is the fixed query. Thanks guys for the recommendations.</p> <pre><code>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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload