title: MYSQL NOT IN function - How to improve and get it faster
body: <p>I have a CRM that one of the queries looks like this one:</p>
  
  <pre><code>SELECT DISTINCT tb1.customerID 
    FROM sales_company AS tb1
   WHERE rdate BETWEEN '2016-02-01' 
                   AND '2016-02-14' 
     AND type='payment' 
     AND customerID 
                NOT IN (SELECT customerID 
                          FROM sales_company 
                         WHERE rdate &lt; tb1.rdate 
                           AND type='payment' 
                         GROUP BY customerID) 
   GROUP BY customerID`
  </code></pre>
  
  <p>With this query, I'm getting how much NEW customers has paid for their first time in the period in the query.</p>
  
  <p>The issue:
  I have a lot of tables with every company so I need to run this query around 30 times every refresh.</p>
  
  <p>Yeah,
  I know it's bad, this system I built is very old (7 years ago) and I cant touch her too much except change this query.</p>
  
  <p>There is some good suggestion you guys know how to improve this query to get the number of rows at the end?</p>
  
  <p>Thanks!</p>