query memory optimisation