Fix memory (estimate) leak and reduce memory usage of GROUP BY and DISTINCT