It seems like this will work, which is very close to what you had:
SELECT MINUTE(date_field) as `minute`, count(id) as count FROM table WHERE date_field > date_sub(now(), interval 5 minute) GROUP BY MINUTE(date_field) ORDER BY MINUTE(date_field);
Pay attention to the added column to show the minute, and the GROUP BY clause, which collects the results at the corresponding minute. Imagine that you had 5 small buckets marked in the last 5 minutes. Now imagine that you threw every line that was 4 minutes into her own bucket. count () will count the number of records found in each bucket. This is a brief visualization of how GROUP BY works. http://www.tizag.com/mysqlTutorial/mysqlgroupby.php seems like a worthy entry in GROUP BY if you need more information.
If you run this and the number of entries per minute seems too high, you will need to fix some problems. Try replacing COUNT (id) with MAX (date_field) and MIN (date_field) so you can understand what dates it captures. If MIN () and MAX () are in range, you may have more data written to your database than you understand.
You can also check that you do not have dates in the future, since all of them will now be> (). The MIN () / MAX () checks mentioned above should also identify this if this is a problem.
gfortune
source share