Which one is the best way to optimize the performance if optimization available,
Option 1:
public without sharing class AccountTrigger extends TriggerHandler{
List<Id> accountIds;
public override void afterUpdate() {
if (accountIds.size() > 0) {
List<Case> casesToUpd = [select Id,AccountId from Case where AccountId in :accountIds];
for (Case c: casesToUpd) {
if((acctToAcctInfo.get(c.AccountId).split('%%%%%'))[0]==''||(acctToAcctInfo.get(c.AccountId).split('%%%%%'))[0]=='null'){
c.Account_Important_Info__c = '' ;
}else{
c.Account_Important_Info__c = (acctToAcctInfo.get(c.AccountId).split('%%%%%'))[0] ;
}
if((acctToAcctInfo.get(c.AccountId).split('%%%%%'))[1]=='' || (acctToAcctInfo.get(c.AccountId).split('%%%%%'))[1]=='null'){
c.Account_DTS_Info__c = '';
}else{
c.Account_DTS_Info__c = (acctToAcctInfo.get(c.AccountId).split('%%%%%'))[1] ;
}
}
BatchApex btch = new BatchApex(casesToUpd);
Database.executeBatch(btch,200);
}
}
}
global class BatchApex implements Database.Batchable<Case>{
global final List<Case> cList;
global BatchApex (List<Case> lst){
//retrieve the case details from Account Trigger
cList = new List<Case>(lst);
}
global Iterable<Case> start(Database.BatchableContext BC){
return cList;
}
global void execute(Database.BatchableContext BC, List<Case> scope){
update scope;
}
global void finish(Database.BatchableContext BC){
}
}
Option 2:
public without sharing class AccountTrigger extends TriggerHandler{
List<Id> accountIds;
public override void afterUpdate() {
if (accountIds.size() > 0) {
BatchApex btch = new BatchApex(accountIds);
Database.executeBatch(btch,200);
}
}
}
global class BatchApex implements Database.Batchable<Case>{
global final List<Id> acountIds;
global BatchApex (List<Id> lst){
//retrieve the Account details from Account Trigger
acountIds = new List<Id>(lst);
}
global Database.QueryLocator start(Database.BatchableContext BC){
String query = 'select Id,AccountId from Case where AccountId in :accountIds';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<Case> casesToUpd){
for (Case c: casesToUpd) {
if((acctToAcctInfo.get(c.AccountId).split('%%%%%'))[0]==''||(acctToAcctInfo.get(c.AccountId).split('%%%%%'))[0]=='null'){
c.Account_Important_Info__c = '' ;
}else{
c.Account_Important_Info__c = (acctToAcctInfo.get(c.AccountId).split('%%%%%'))[0] ;
}
if((acctToAcctInfo.get(c.AccountId).split('%%%%%'))[1]=='' || (acctToAcctInfo.get(c.AccountId).split('%%%%%'))[1]=='null'){
c.Account_DTS_Info__c = '';
}else{
c.Account_DTS_Info__c = (acctToAcctInfo.get(c.AccountId).split('%%%%%'))[1] ;
}
}
update casesToUpd;
}
global void finish(Database.BatchableContext BC){
}
}
I am expecting the optimize one but didn't know how to decide which one is the optimize one. getting the Case from the Trigger and pass to the Batch in the Option 1 however in the Option 2 I am getting the Case records in the start method(QueryLocator) of the Batch. Which one is the most optimize one?