function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Prajapati.LakhanPrajapati.Lakhan 

Governor limits for sub query in query locator (Batch apex)?

Hi,

 

I know that we can iterate at most 50M rows using query locator, does anybody have any idea about SOQL rows limit for sub query used in query locator? 

 

Thanks in advance.

 

Thanks,

Lakhan

Best Answer chosen by Admin (Salesforce Developers) 
HariDineshHariDinesh

Hi,

 

By using below point you can understand

 

In a SOQL query with parent-child relationship sub-queries, each parent-child relationship counts as an additional query. These types of queries have a limit of three times the number for top-level queries.

The row counts from these relationship queries contribute to the row counts of the overall code execution.

In addition to static SOQL statements, calls to the following methods count against the number of SOQL statements issued in a request.

 

  • Database.countQuery
  • Database.getQueryLocator
  • Database.query

From above statement i can clarify one point:

 

For example your org contain  only 2 Account and each have 10 Contacts.

 

If you execute this with sub Query like below

Select Id, Name, (Select Id, Name from Contacts) From Account

 

Then the total number of Query rows will be 22 (2+10+10).

 

From above analogy we can understand like this

 

If your org contain 40M accounts and each have 1 contact.

Then in this scenario you can use sub query up to 25M only.

 

Like this Select Id, Name, (Select Id, Name from Contacts) From Account limit 25M

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_gov_limits.htm

 

All Answers

Alex.AcostaAlex.Acosta
Total number of records retrieved by SOQL queries50,000

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_gov_limits.htm

HariDineshHariDinesh

Hi,

 

By using below point you can understand

 

In a SOQL query with parent-child relationship sub-queries, each parent-child relationship counts as an additional query. These types of queries have a limit of three times the number for top-level queries.

The row counts from these relationship queries contribute to the row counts of the overall code execution.

In addition to static SOQL statements, calls to the following methods count against the number of SOQL statements issued in a request.

 

  • Database.countQuery
  • Database.getQueryLocator
  • Database.query

From above statement i can clarify one point:

 

For example your org contain  only 2 Account and each have 10 Contacts.

 

If you execute this with sub Query like below

Select Id, Name, (Select Id, Name from Contacts) From Account

 

Then the total number of Query rows will be 22 (2+10+10).

 

From above analogy we can understand like this

 

If your org contain 40M accounts and each have 1 contact.

Then in this scenario you can use sub query up to 25M only.

 

Like this Select Id, Name, (Select Id, Name from Contacts) From Account limit 25M

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_gov_limits.htm

 

This was selected as the best answer
Prajapati.LakhanPrajapati.Lakhan

Thanks Hari for the quick response.  Little bit worried about the query locator, should i move the sub queries in execute method to leverage the full capacity of query locator? what should be the best practise.

 

 

HariDineshHariDinesh

Hi,

 

Yes, as per the above points if you want to get the Full Capacity of Get Query locator (50M) then it is better to move to execute method.

 

Before going to this implementation:

FYI

You need to understand one more point.

Normally select query can get only 50,000 records, but get query locator can get 50M records.

 

So be cautious and plan your code/Query in Execute method accordingly.