There's no need to load all of both datasets into memory.
If you load the names from the DB into a hash (which DBI will do for you in one hit--select names from table; fetchall_hashref), then you can process your file line by line.