Optimization of COUNT queries