title: Query to find duplicate timestamps MySQL
body: <p>I have written the following query to find duplicate timestamps within a date range with the intent of then deleting those duplicates with a larger id. However this selection never completes.</p>
  
  <pre><code>SELECT 
      *
  FROM
      data
  WHERE
  id NOT IN (SELECT 
          MIN(id)
      FROM
          data
      WHERE
          datapoint_name LIKE 'Temp%'
              AND timestamp BETWEEN '2012-07-31' AND '2012-08-03'
      group by timestamp , datapoint_name)
   AND datapoint_name LIKE 'Temp%'
   AND timestamp BETWEEN '2012-07-31' AND '2012-08-03';
  </code></pre>
  
  <p>I find it strange because the individual components run very fast and there are not that many rows. Specifically:</p>
  
  <ul>
  <li>the SELECT MIN(ID) ... GROUP BY subquery returns 476 rows in .7 sec.</li>
  <li>the outer SELECT * without id NOT IN() returns 490 rows in 0.001 sec.</li>
  </ul>
  
  <p>In other words there are 14 duplicates, but the NOT IN() operation seems to be taking an exorbitant amount of time. In fact, I've never had the patience to see if it will ever finish. What can I do to speed this up? Have I done something fundamentally wrong? </p>