Sub topic 7
Choosing and tuning the batch size
This chapter assumes you can write a basic Database.Batchable class and know the main Apex governor limits. It covers how the scope size passed to Database.executeBatch affects speed, limit usage, and failure behavior.
What the batch size controls
The second argument to Database.executeBatch is the scope size. It is the number of records passed to each execute call. Each call is one transaction, so governor limits reset between calls.
Id jobId = Database.executeBatch(new AccountRatingBatch(), 200);If you leave the argument out, the default is 200. The maximum is 2,000 for a QueryLocator. An Iterable source also accepts a scope size, but the 2,000 cap applies to both.
A job over 1,000,000 records with scope 200 runs 5,000 execute calls. With scope 50 it runs 20,000.
Trade-offs
| Smaller scope | Larger scope |
|---|---|
| More headroom per transaction for SOQL, CPU, and heap | Fewer transactions and less queue overhead |
| One failure rolls back fewer records | One failure rolls back more records |
| Slower overall, more `execute` calls | Faster overall, if limits allow |
| Fits callouts and heavy triggers | Fits simple field updates |
Each execute call has its own limits. These include 100 SOQL queries, 150 DML statements, 10,000 ms of CPU time, and 6 MB of heap. Triggers, flows, and other automation fired by your DML count against the same limits. A scope of 200 that works on a plain object can fail on one with heavy automation.
Tuning in practice
- Start with the default of 200.
- Run the job against a realistic data volume in a sandbox.
- Read the errors. A
Too many SOQL queries,Apex CPU time limit exceeded, or heap error means the scope is too large for the work done per record. - Lower the scope, for example to 100, then 50, then 10, and rerun.
- If the job runs clean with plenty of headroom, try raising the scope to reduce run time.
Use Limits.getCpuTime() and Limits.getQueries() inside execute during testing to see how much of each limit one call uses.
public void execute(Database.BatchableContext bc, List<Account> scope) {
// ... process scope ...
System.debug('CPU ms: ' + Limits.getCpuTime() + ' of ' + Limits.getLimitCpuTime());
System.debug('Queries: ' + Limits.getQueries() + ' of ' + Limits.getLimitQueries());
}Cases that need a small scope
- Callouts. A transaction allows 100 callouts, and each can take seconds. Use a scope well under 100 if you make one callout per record, or batch the payloads.
- Heavy triggers or flows on the object. Their cost multiplies by the scope size.
- Large records. Long text fields or many fields in the query raise heap use.
- Row locks. Smaller scopes hold locks for less time, which reduces
UNABLE_TO_LOCK_ROWerrors.
Exercise
A batch job over 600,000 records uses scope 200 and fails with "Apex CPU time limit exceeded" in some execute calls. What should you change first, and roughly how many execute calls will the job make if you lower the scope to 50?
Show answer
Lower the scope size, since CPU time is counted per transaction and fewer records per call means less work per call. At scope 50, the job makes 600,000 / 50 = 12,000 execute calls, up from 3,000 at scope 200.