title: Make more efficient SELECT query with LEFT join and conditions on the Join
body: <p>This question is on <a href="https://stackoverflow.com/questions/26021590/left-join-logic-not-as-expected">the back of one I asked earlier today</a>. The answer I got solved my problem in that, by limiting rows returned I can see that it does what I want. </p>
  
  <p>But now when I try to run the whole query, with no limit, with the purpose of exporting into excel for analysis, I seem unable to get anywhere. I get kicked off SQL in that MySQL workbench asks me for my password again and the query stops running. I'm not sure if that piece of information is something else happening and a diversion from my real question which is "How can I get this query to run faster, if at all?" Currently it runs for at least 5 minutes before "kicking me off".</p>
  
  <p>When I EXPLAIN the query here is what is provided:</p>
  
  <pre><code>1   SIMPLE  co  ALL                 185610  Using temporary; Using filesort
  1   SIMPLE  my  ref PRIMARY PRIMARY 4   bm_emails.co.id 23  
  1   SIMPLE  nvk eq_ref  PRIMARY PRIMARY 4   bm_emails.co.id 1   
  </code></pre>
  
  <p>Presumably the temporary table is causing the "Using Temporary" is a problem but I'm unsure how to get around it while maintaining my query.
  The actual query is here:</p>
  
  <pre><code>SELECT 
      co.email,
      nvk.nvk_medium,
      CAST(MIN(co.created) AS DATE) AS first_contact,
      MIN(CASE WHEN my.my_id = 581 THEN my.data END) AS WA_Created,
      MIN(CASE WHEN my.my_id = 3347 THEN my.data END) AS WA_Upgraded
  FROM bm_emails.cid208 co
  LEFT JOIN bm_emails.my208 my ON co.id = my.eid AND (my_id = 581 OR my_id = 3347)
  LEFT JOIN bm_emails.nvk208 nvk ON nvk.eid = co.id
  GROUP BY email
  </code></pre>