title: Display last value for each user in SQL (fast)
body: <p>I'm trying to find if the last action for a user in the last 24 hours was in and not out (it's a software for knowing which persons are still in the building in case of emergency). The table has around 2 milion rows, we have around 3000 users.</p>
  
  <p>The only solution I found was the one below, It works but the problem is it takes 15 seconds...</p>
  
  <pre><code>SELECT TIME, firstname, lastname, dept
  FROM DAY
  WHERE event_point_id IN (20, 22, 24, 26, 28, 30)
    AND id IN (SELECT MAX(id)
               FROM DAY
               GROUP BY pin)
  GROUP BY pin
  ORDER BY TIME ASC
  </code></pre>
  
  <p>DAY is a view with the following details</p>
  
  <pre><code>SELECT id, pin, event_point_id, time, firstname, lastname, dept
  FROM acc_monitor_log
  WHERE (((TO_DAYS(NOW()) - TO_DAYS(time)) &lt; 1) AND (pin &lt;&gt; '--'))
  </code></pre>
  
  <p>What could be the options to speed up this?</p>