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
harsha vardhan vasa 9harsha vardhan vasa 9 

Hi Team, getting apex cpu time limit exceeded for the bacth class with batch size 2

Hi Team, 
below is the code snippet. getting cpu time limit exceeded for the batch size 2 also. any optimization can be done to the following code. 
the premise object scoped records are 6.7 million.
         public class batchUpdatetable implements Database.Batchable<sObject> {

Id resedentialRT = Schema.SObjectType.EI_Site__c.getRecordTypeInfosByName().get('Residential').getRecordTypeId();         
   public Database.QueryLocator start(Database.BatchableContext BC){
       String Query;
      return database.getquerylocator('select id,(select id from Site_Service_Agreements__r where recordtypeId=:resedentialRT ),CENSUS_TRACT__c,Cal_Enviro_Version_EI__c,CS_GT_Legacy_DAC__c from EI_PREMISE__c LIMIT 99999');
     
   }

   public void execute(Database.BatchableContext BC, List<EI_PREMISE__c> scope){
       List<EI_PREMISE__c> prToUpdate = new List<EI_PREMISE__c>();
       list<EI_CensusTract_temp__c> templist = new list<EI_CensusTract_temp__c>([select id,Cal_Enviro_Version_EI__c,Census_Tract_EI__c,CS_GT_Legacy_DAC__c from EI_CensusTract_temp__c]);
       map<string,EI_CensusTract_temp__c> tempmap = new map<string,EI_CensusTract_temp__c>();
         for(EI_CensusTract_temp__c t:templist){
            tempmap.put(t.Census_Tract_EI__c,t); 
         }
       for(EI_PREMISE__c s : scope){
         if(tempmap.containsKey(s.CENSUS_TRACT__c)){
             
             s.CS_GT_Legacy_DAC__c = tempmap.get(s.CENSUS_TRACT__c).CS_GT_Legacy_DAC__c;
             s.Cal_Enviro_Version_EI__c = tempmap.get(s.CENSUS_TRACT__c).Cal_Enviro_Version_EI__c;
             //s.DAC_CA__c = tempmap.get(s.CENSUS_TRACT__c).DAC_CA__c;
            
             prToUpdate.add(s);
         }
     }
     update prToUpdate;
    }

   public void finish(Database.BatchableContext BC){
   }
}
harsha vardhan vasa 9harsha vardhan vasa 9
please ignore the limit from query" LIMIT 99999"
AnkaiahAnkaiah (Salesforce Developers) 
Hi Harsha,

try with below code.
public class batchUpdatetable implements Database.Batchable<sObject> {

 Id resedentialRT = Schema.SObjectType.EI_Site__c.getRecordTypeInfosByName().get('Residential').getRecordTypeId();         
   public Database.QueryLocator start(Database.BatchableContext BC){
       String Query;
      return database.getquerylocator('select id,(select id from Site_Service_Agreements__r where recordtypeId=:resedentialRT ),CENSUS_TRACT__c,Cal_Enviro_Version_EI__c,CS_GT_Legacy_DAC__c from EI_PREMISE__c where CENSUS_TRACT__c !=Null');
     
   }

   public void execute(Database.BatchableContext BC, List<EI_PREMISE__c> scope){
       List<EI_PREMISE__c> prToUpdate = new List<EI_PREMISE__c>();
      
       map<string,EI_CensusTract_temp__c> tempmap = new map<string,EI_CensusTract_temp__c>();
         
		 set<string> ctids = new set<string>();
		 for(EI_PREMISE__c s : scope){
		 ctids.add(s.CENSUS_TRACT__c);
		 }
		 
		 for(EI_CensusTract_temp__c t:select id,Cal_Enviro_Version_EI__c,Census_Tract_EI__c,CS_GT_Legacy_DAC__c from EI_CensusTract_temp__c where Census_Tract_EI__c=:ctids] ){
		  tempmap.put(t.Census_Tract_EI__c,t); 
		 }
       for(EI_PREMISE__c s : scope){
	   
         if(tempmap.containsKey(s.CENSUS_TRACT__c)){
             
             s.CS_GT_Legacy_DAC__c = tempmap.get(s.CENSUS_TRACT__c).CS_GT_Legacy_DAC__c;
             s.Cal_Enviro_Version_EI__c = tempmap.get(s.CENSUS_TRACT__c).Cal_Enviro_Version_EI__c;
             //s.DAC_CA__c = tempmap.get(s.CENSUS_TRACT__c).DAC_CA__c;
            
             prToUpdate.add(s);
         }
     }
     update prToUpdate;
    }

   public void finish(Database.BatchableContext BC){
   }
}

If this helps, Please mark it as best answer.

Thanks!!