title: SQL, Unused LEFT JOINs slowing down, Optimizer not working?
body: <p>I have a simple SQL statement. Some of the LEFT JOINs aren't used in the result set. Why is MySQL 5.5 slowing down the execution on the second version of my SQL, although the JOINs are completely irrelevant for the result? Why isn't the LEFT JOIN being ignored?</p>
  
  <p>Exectime: 0.03s (no JOINs)</p>
  
  <pre><code>SELECT SQL_NO_CACHE count(o.orderid), o.orderid
  FROM
      customerorder o
  LIMIT 0,20
  </code></pre>
  
  <p>Exectime: 0.20s (irrelevant JOINs)</p>
  
  <pre><code>SELECT SQL_NO_CACHE count(o.orderid), o.orderid
  FROM
      customerorder o
      LEFT JOIN object o1 ON o1.objectid = o.orderid
      LEFT JOIN object o2 ON o2.objectid = o1.objectid
      LEFT JOIN object o3 ON o3.objectid = o2.objectid
      LEFT JOIN object o4 ON o4.objectid = o3.objectid
      LEFT JOIN object o5 ON o5.objectid = o4.objectid
  LIMIT 0,20
  </code></pre>
  
  <p><strong>Edit/"Solution":</strong> It's not being ignored, because the JOINs are relevant :-) They could increase the count(). So a solution to the problem is something like this:</p>
  
  <pre><code>SELECT SQL_NO_CACHE (select count(*) from customerorder) as count, o.orderid
  FROM
      customerorder o
      left JOIN object o1 ON o1.objectid = o.orderid
      left JOIN object o2 ON o2.objectid = o1.objectid
      left JOIN object o3 ON o3.objectid = o2.objectid
      left JOIN object o4 ON o4.objectid = o3.objectid
      left JOIN object o5 ON o5.objectid = o4.objectid
  LIMIT 0,20
  </code></pre>