Software Development

How do we handle Salesforce Governor Limits when processing millions of records in Batch Apex?

SU Asked by Susan Davis · 22-02-2024
0 upvotes 12,854 views 0 comments
The question

I am currently designing a data cleansing job that needs to touch roughly 5 million records. I’m worried about hitting the 10,000ms CPU timeout and the SOQL query limits. What are the best patterns for "Chaining" batches or using the "Iterable" interface to stay within the multi-tenant constraints? 

3 answers

0
EL
Answered on 25-02-2024

To handle 5 million records, you must ensure your start() method is highly efficient. Using a QueryLocator is usually better than an Iterable for large sets because it can bypass the 50,000 record limit (up to 50 million). In the execute() method, keep your batch size small—around 200 is standard, but you can drop it to 50 if your logic is heavy. To stay under the CPU limit, move any non-essential logic to @future methods or Queueable Apex if it doesn't need to be part of the atomic transaction. If you need to "chain" batches, remember that you can only start one new batch from the finish() method of another. This "sequential batching" pattern is the safest way to avoid the "Too many concurrent batches" error. 

0
RI
Answered on 27-02-2024

Does using the "Database.Stateful" interface significantly increase the execution time due to the serialization overhead? 

JO 01-03-2024

It does add some overhead, Richard, but for 5 million records, the impact is usually negligible compared to the benefit of tracking progress or errors across segments. Just be careful not to store massive collections in your variables; only keep primitive counters or small maps to minimize the state size being passed between executions.

0
MA
Answered on 02-03-2024

Always remember to use "Aggregate Queries" if you only need sums or counts to keep the SOQL count low. 

SU 03-03-2024

True, Mary. Leveraging the database engine for math instead of doing it in Apex loops is a lifesaver for CPU time.

Share your thoughts

Your email address will not be published. Required fields are marked (*)

Professional Counselling Session

Still have questions?
Schedule a free counselling session

Our experts are ready to help you with any questions about courses, admissions, or career paths. Get personalized guidance from industry professionals.

Request a Call Back

Search Online

We Accept

We Accept

Follow Us

"PMI®", "PMBOK®", "PMP®", "CAPM®" and "PMI-ACP®" are registered marks of the Project Management Institute, Inc. | "CSM", "CST" are Registered Trade Marks of The Scrum Alliance, USA. | COBIT® is a trademark of ISACA® registered in the United States and other countries.

Book Free Session