Q:

Salesforce Relating objects without Lookup ID

We have an app that pulls data from our rails server and loads it into SalesForce via batch jobs. The object that we load data into MyObj has 2 lookup fields for relating it to Contacts and Leads. The problem is that rails knows nothing about Contacts or Leads therefore it cannot set these lookup fields. The only relationship between MyObj and a Contact/Lead is an email address. 
The problem that arises is that we end up having to query all Contacts and Leads, grab the ID and then set the lookup field in MyObj. When we ran checkmarx on this, it complained about "Queries with no Where clause or no LIMIT clause". 
So the question is, how can we relate objects, without knowing the IDs of SalesForce objects and do this without exceeding the governor limits?
PS.
External IDs are not an option since we do not have a relationship to Contacts/Leads in the first place.

A:

This is a pretty normal scenario, and an email address does pretty well for this purposes.  Tricky part is figuring out what you're going to do when the email address matches multiple contacts and/or leads.
Regardless of how you implement this, you'll end up needing to query all contacts and leads with an email address matching the data you pull from your rail server and use that to set the lookup on your object.
Assuming you're processing everything in one massive call and need to have one massive call to get the data down a before trigger is an easy option.  Triggers batch processing into sets of 200 records (at the moment at least).  You can query the related contacts and leads there and update them.

