Reduce the memory utilization of the engine when the cardinality of the group by columns is big

This pr contains improvements to the engine that was driven by a user data set that has high group by cardinality. That said, most of the improvements are going to benefit other types of queries as well.