Optimise the amount of queries used when counting