Speed up group_by clause