optimize query that gets the attributes