update counter method in flags to use a sql call instead of iterating in memory