0

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
  }
}
2
  • 2
    Have you tried using the query optimizer to check performance? You have just three parts to your filter 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 '23 CustomIndex 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.
    – Phil W
    Commented Dec 28, 2022 at 13:59
  • 1
    See this documentation and be aware that this self-service is rather new and lacking documentation. See metadata coverage.
    – Phil W
    Commented Dec 28, 2022 at 14:05

1 Answer 1

0

If you need to segment your data 10 times, you could use 10 queuable methods, each method will call a batch from a segment of data. You could segment your data using the soql LIMIT keyword for instance.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .