in reply to millions of records in a Hash
If you don't need the whole hash in memory at the same time,
and i assume you don't
then let the database do the work.
Like this pseudo code:
SELECT FROM foo WHERE (bar=$a);
found a dataset? UPDATE else CREATE