I am running a batch class that queries Line_Item__c
in our production org. The query is executed in start method of Batch class. Since we have a total of more than 12 million records in our database for Line_Item__c
, the execution is being terminated without any error. As a hotfix, I indexed query using specific accounts, which I received by generating report on Line_Item__c
, and it ran within seconds. How can I apply a permanent fix for this? (Batch size is gives as 200 in execute batch from where the class is called)
Here goes my code:
global class Batchclass implements Database.Batchable<SObject>{
public Date invDate;
public Batchclass(Date batInvDate){
invDate = batInvDate;
}
global Database.QueryLocator start(Database.BatchableContext BC){
String query = 'SELECT {around 50 fields} FROM Line_Item__c WHERE Package_Type__c != NULL AND Post_Status__c = \'unposted\' AND Invoice_Date__c = :invDate';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<Line_Item__c> LIList){
if (LIList.size() > 0) {
Apexclassprocess.dataProcess(LIList);
}
}
global void finish(Database.BatchableContext BC) {
// execute any post-processing operations
}
}
Package_Type__c != NULL AND Post_Status__c = \'unposted\' AND Invoice_Date__c = :invDate
. Null checks are non-optimal. Strings and dates can be indexed, through the use of the Winter '23CustomIndex
object. This used to be something that only Salesforce Support could do if you raised a case, but it is now self-service on your custom objects. You have to use the Tooling or Metadata API to handle this, I believe.