optimize aggregations queries