It may be overkill, but you can use a database for this functionality.
Both mysql and Oracle have efficient ways of loading large datasets into a pre-defined table while obeying constraints on those tables. This may not be the fastest solution, but it should ease up on the memory requirements.