Optimizing the count querry