title: Mysql LEFT JOIN gets very slow above 25.000 rows
body: <p>The following query becomes suddenly extremly slow (seems not to finish at all) with more than approx <strong>25.000</strong> rows in table a (tried with adding limit x to the query):</p>
  
  <pre><code>select a.*, b.column 
    from table_a a
  left join table_b b 
    on (a.message_id = b.message_id) 
  where date_sub(curdate(), interval 30 day) &lt; a.date 
    and ( 
          b.message_id is NULL or 
          ( 
            b.message_id is not NULL 
            and a.message_id = b.message_id 
            and b.redeemed &gt; 0 
          )
        )
  order by a.id DESC
  </code></pre>
  
  <p>there is an index on both <code>a.message_id</code> and <code>b.message_id</code>. table_a will have much more rows than <code>table_b</code></p>
  
  <p>I understand that the left join in combination with <strong>is NULL</strong> causes the mysql server reaches its limit, but I don't know how to rewrite the query to avoid the left join.</p>
  
  <p>Basically I want to query table a and check if there is a matching entry in table b. If that is the case I want to consolidate the date from table b into the result.</p>
  
  <p>Any help is very appreciated!</p>