Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed help speeding up a MySQL query
    primarykey
    data
    text
    <p>I need a query that quickly shows the articles within a particular module (a subset of articles) that a user has NOT uploaded a PDF for. The query I am using below takes about 37 seconds, given there are 300,000 articles in the Article table, and 6,000 articles in the Module.</p> <pre><code>SELECT * FROM article a INNER JOIN article_module_map amm ON amm.article=a.id WHERE amm.module = 2 AND a.id NOT IN ( SELECT afm.article FROM article_file_map afm INNER JOIN article_module_map amm ON amm.article = afm.article WHERE afm.organization = 4 AND amm.module = 2 ) </code></pre> <p>What I am doing in the above query is first truncating the list of articles to the selected module, and then further truncating that list to the articles that are not in the subquery. The subquery is generating a list of the articles that an organization has already uploaded PDF's for. Hence, the end result is a list of articles that an organization has not yet uploaded PDF's for. </p> <p>Help would be hugely appreciated, thanks in advance!</p> <p><strong>EDIT 2012/10/25</strong></p> <p>With @fthiella's help, the below query ran in an astonishing 1.02 seconds, down from 37+ seconds! </p> <pre><code>SELECT a.* FROM ( SELECT article.* FROM article INNER JOIN article_module_map ON article.id = article_module_map.article WHERE article_module_map.module = 2 ) AS a LEFT JOIN article_file_map ON a.id = article_file_map.article AND article_file_map.organization=4 WHERE article_file_map.id IS NULL </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.
    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