optimize pharmgx query