1

Finding mixed results within web searches, trying to locate a concise answer as to how many records can a batch process.

I am creating an apex batch that will run daily. It will query around 55,000 records the first time that it runs. Daily runs after that will query 500-1000 records.

Would limiting the scope of the execute batch method to say 1 potentially cause reaching governor limits? Does this mean the execute method will run 55,000 times?

5

The only practical limit on batch size is how long you want your batch to run. A batch can iterate over millions of records.

Would limiting the scope of the execute batch method to say 1 potentially cause reaching governor limits?

Setting a scope size of 1 will reduce your risk of governor exceptions, but increase overall run time of your job.

Does this mean the execute method will run 55,000 times?

Yes, if you want to process 55k records and you set the scope size to 1, your execute method will get called 55k times.

| improve this answer | |
  • Perfect, exactly what I was looking for. Is there a simple way to estimate the approximate time for how long a scheduled job will take to complete? – ebar Oct 7 '19 at 16:43
  • 1
    Depends entirely on what type of processing you are doing. A single Apex transaction cannot take more than 10 seconds CPU time, but this limit is extended to 60 seconds for async. Also, some operations take clock time without counting against this limit, such as callouts or long running queries to the database. Given that the maximum cumulative wait time for callouts is 120 seconds, you could run over 3 minutes per execute. – Adrian Larson Oct 7 '19 at 16:49
  • I removed my comment @Adrian, got mixed up – Glen De Marcos Oct 7 '19 at 16:54
1

The maximum number of SObjects a batch can process depend on the type of object returned from start.

  • Iterable - the maximum number is 50000 since this is the maximum number of rows that you can query from the database in a session (the iterable is an in-memory representation of the whole set of objects to be iterated)
  • Database.QueryLocator - the maximum number is 50 million records.

You'll find these limits described in the documentation.

NB: You can use an iterator-based batch to process non-SObject data in which case the SObject limits don't apply.

| improve this answer | |
  • 1
    Some more additions to Phil's post. */ If the start method of the batch class returns a QueryLocator, the optional scope parameter of Database.executeBatch can have a maximum value of 2,000. */ If set to a higher value, Salesforce chunks the records returned by the QueryLocator into smaller batches of up to 2,000 records. */ If the start method of the batch class returns an iterable, the scope parameter value has no upper limit. – Arabinda Oct 8 '19 at 0:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.