optimize the query to get the keywords per group