Optimized row grouping