Move count query to database client.