optimize group query