function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Mass record comparison in Salesforce using Batch Apex


I am in a need to compare two objects having millions of records in both of them. Both objects have same fields and I need to compare for unique records; lets say for one field 'Amount', I need to take records one by one from Obj1 (will also contain millions of records) and need to compare it with the millions records of Obj2 in Amount fields. If there is a match, I need to do some field update on Obj2. Only SOQL query will not help me because it will hit govern limit of salesforce.
Is there any way to achieve this mass record comparison using Apex keeping code optimization, govern limit and time complexity in mind. 

I doubt you'll be able to do this in a way that will scale. Processing millions of records from obj1 on their own isn't an issue - batch apex can handle up to 50 million records, but you won't be able to iterate across millions of records from obj2, as you'd need to do this inside a single transaction that is processing a number of object a records, so you'll still hit the governor limits. 
Hi bob, 

Thanks for the reply.
I have tried to use chaining of batch classes but it doesn't work. The problem is I can't call another batch class (say BatchClassB for Obj2) from START or EXECUTE method of BatchClassA (for Obj1). When I define the call of BatchClassB from BatchClassA's FINISH method, It is executing once only (i.e after processing all records of Obj1). I need something to call BatchClassB (where compression will be done) after taking one record from Obj1 using BatchClassA.

Have you any Idea on this or any other alternative you can suggest. 

Thanks again !! 

I haven't I'm afraid - you are looking at billions of record accesses which I doubt you'll be able to do within the constraints of the platform.