title: MySQL: Find records matching in two tables A & B where B is massive
body: <p>The following works fine but is too slow . I just need to know that there are matching records in the Table B so is there any trick?</p>
  
  <p>(curiously the opposite search: find records that don't join (IS NULL) is very quick)</p>
  
  <pre><code>SELECT
    TableA.id
  FROM
    TableA
    LEFT JOIN
      TableB 
    ON
      TableB.TableA_id = TableA.id
  WHERE
    TableB.id IS NOT NULL and TableA.clientid=13 ;
  </code></pre>
  
  <p>Clientid is now indexed but it is this subclause which drastically slows the query from very quick to minutes.</p>
  
  <p>(03May12 I have now tried this same query on an identical database on a much more powerful server and the query takes a second. So it would seem my problem is that my original server is totally overloaded , ironically the above query was being used to identify records which we could delete to reduce the size of the tables. I will have to re-build the database on a faster server , clean it up and then regenerate it on the "slow" server)</p>