Add indexes on event and reset timestamps for sorting
Implement limiting and counting to improve large database performance
Move count SQL generation into helper method so it is easier to override