Filter at the SQL level, rather than looping through the entire set