made it more efficient

combining the SQL query reduced completion time of this chunk of the
code by a factor of 10,000!