Optimize count queries and add comment