+ Start a Discussion
anusha reddy 158anusha reddy 158 

Two Large Queries in Batch Apex returning at least 1lakh records each

I've two objects(Obj1 & Obj2), i will have to fetch the data in obj1. And based on the fields data in obj1, i've to query the data in Obj2.
global Class batchapex1 implements Database.Batchable<sObject>{
    Public Set<String> passeddata{get; set;}
    global Database.QueryLocator start(Database.BatchableContext BC){
        return Database.getQueryLocator([Select fld1,fld2,fld3 from Obj1 where fld1 in: passeddata]);
    global void execute(Database.BatchableContext BC, List<Obj1> Obj1List){
    global void finish(Database.BatchableContext BC){
But here based on the Obj1 query data, i'will have to fetch the obj2 data like below
Set<String> obj1data= new Set<String>();
for(Objj1 i:Obj1List){
if(obj1data.size() > 0){
    Obj2List = [Select fld1,fld2,fld3 from Obj2 where fld1 in: obj1data];
//some calculations happen comparing the obj1 data & obj2 data and finally update obj1 data

The concern here is two objects returns large data around 1lakh records each. i would like to have both the queries are returned through querylocator as many no. of records(50M) can be queried through query locator. Please advise
Hi Anusha,

As you mentioned the query in start method can return more than 1 lakh records. Thats fine. It would be handled by query locator.
But when the execute method will get called, it won't be called with all the 1 lakh record at once. Only a set(batch) of record say 200 depending on batch size will be send to execute method.
So, the no of record fetched by query inside the execute method won't return 1 lakh record, it will only return 200 (batch size) records at a time.
It will work fine.

Hope it helps!
ShirishaShirisha (Salesforce Developers) 
Hi Anusha,


Yes,you need to use the queryLocator to process around 50 million records in batch apex.Also,as suggested in the above comment you can work upto 1 lakh in Start method.


Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Shirisha Pathuri