2

Why the Salesforce Governor Limits cheatsheet show the following?

Total number of records retrieved by Database.getQueryLocator ---> 10000

What does this mean?

The limits are reset for every execute(), not for every start() execution, so the records are retrieved in a single start() call and I'm able to retrieve much more than 10k records by a getQueryLocator.

What does the trick?

3 Answers 3

6

According to the documentation, in a batch start() method the governor limit is bypassed. The governor limit of 10,000 in the limits cheatsheet still applies in execute() and finish() and other methods.

Use the Database.QueryLocator object when you are using a simple query (SELECT) to generate the scope of objects used in the batch job. If you use a querylocator object, the governor limit for the total number of records retrieved by SOQL queries is bypassed. For example, a batch Apex job for the Account object can return a QueryLocator for all account records (up to 50 million records) in an organization. Another example is a sharing recalculation for the Contact object that returns a QueryLocator for all account records in an organization.

5
  • 1
    Thanks.. I was forgetting that in a start() method, using getQueryLocator the limits are not enforced. (it was a purely theorical question, de facto I knew and I experienced those limits are not enforced.)
    – user9959
    Commented Jan 30, 2015 at 11:22
  • So, I simply ignored that we can use getQueryLocator outside a batch start() !
    – user9959
    Commented Jan 30, 2015 at 11:25
  • Exactly. Using a standard set controller, you can pass in a query locator, but it has to have at most 10000 rows.
    – sfdcfox
    Commented Jan 30, 2015 at 15:50
  • @sfdcfox if we return a QueryLocator from the start() method, and the limits are bypassed, does that mean that we can do any kind of queries in the start() method and they will not count against the limit? Or is the limit bypass only valid for a QueryLocator that is constructed using a String, and all other queries count against the limits as usual? Commented Apr 11, 2018 at 15:05
  • @WillemMulder You're still limited to 50,000 normal query rows, both inline and database query methods, but the QueryLocator you return from the start method won't count. You can use inline queries or strings with getQueryLocator (return database.getquerylocator([select...]) works just fine).
    – sfdcfox
    Commented Apr 11, 2018 at 15:39
2

Its worth noting that while the governor limits are bypassed in the start() method, there is still an overall limit of 50 million records, per the docs:

When you’re using a simple query (SELECT) to generate the scope of objects in the batch job, use the Database.QueryLocator object. If you use a QueryLocator object, the governor limit for the total number of records retrieved by SOQL queries is bypassed. For example, a batch Apex job for the Account object can return a QueryLocator for all account records (up to 50 million records) in an org. Another example is a sharing recalculation for the Contact object that returns a QueryLocator for all account records in an org.

0

Here is a summary of the limits:

Single SOQL Query: 50,000 records.

Database.getQueryLocator in Batch Apex:

  • Query Locator: Up to 50 million records, processed in manageable chunks.

SOQL Query in Batch Apex:

  • Standard SOQL in start Method: 50,000 records. Using Database.getQueryLocator: Enables handling up to 50 million records by breaking them into smaller batches.

You must log in to answer this question.