1

Most of us know this is a very basic question but I little confused here again, As per the documentation here, it says that parent-child query row count is added to the overall row count in the execution context(50000).

But when I executed below code and while verifying the limits,

I found the record count of the child sub-queries isn't added to total row count. Not sure I'm checking wrongly or salesforce has made some changes in this area some time back.

(In Execute anonymous window)

system.debug('Limits.getLimitAggregateQueries() '+Limits.getLimitAggregateQueries());
system.debug('Limits.getLimitQueries() '+Limits.getLimitQueries());
system.debug('Limits.getLimitQueryRows() '+Limits.getLimitQueryRows());


for(Account acc : [select id,name, 
                   (Select id,name from contacts limit 10),
                   (Select id,name,Product_Quantity__c from Customer_Order__r limit 10)
                   from Account 
                    WHERE 
                  ID IN ('0019000001RiFFI', '0019000001LELy9')]){

                      system.debug('acc name: '+acc.name);
                      system.debug('acc Contacts: '+acc.contacts.size());
                      system.debug('acc customer orders: '+acc.Customer_Order__r.size());
}

system.debug('Limits.getAggregateQueries() '+Limits.getAggregateQueries());
system.debug('Limits.getQueries() '+Limits.getQueries());
system.debug('Limits.getQueryRows() '+Limits.getQueryRows());

In debug logs

In debug logs

  1. If these child sub-query records count is not added to total row count(50000) in execution context, Do these are added to Aggregate query row limits? Or what does it mean ?

    These types of queries have a limit of three times the number for top-level queries

  2. How this below statement can be true in contrast with above screenshot result,

    "In a SOQL query with parent-child relationship subqueries, each parent-child relationship counts as an extra query."

    But It shows as only one query consumed in above screenshot.

SF docs link here

  • @andrian larson Thanks for your response – SFDC_Igle May 31 '19 at 15:59
0

In a SOQL query with parent-child relationship subqueries, each parent-child relationship counts as an extra query

Yes, It is applied when you do query like this:-

system.debug('Limits.getLimitAggregateQueries() '+Limits.getLimitAggregateQueries());
system.debug('Limits.getLimitQueries() '+Limits.getLimitQueries());
system.debug('Limits.getLimitQueryRows() '+Limits.getLimitQueryRows());

List<Account> acc= [select id,name, (Select id,name from contacts limit 10) from Account limit 10000];

system.debug('Limits.getAggregateQueries() '+Limits.getAggregateQueries());
system.debug('Limits.getQueries() '+Limits.getQueries());
system.debug('Limits.getQueryRows() '+Limits.getQueryRows());

Debugs are:-

enter image description here

If you are doing like this:-

system.debug('Limits.getLimitAggregateQueries() '+Limits.getLimitAggregateQueries());
system.debug('Limits.getLimitQueries() '+Limits.getLimitQueries());
system.debug('Limits.getLimitQueryRows() '+Limits.getLimitQueryRows());


for(Account acc : [select id,name, 
                   (Select id,name from contacts limit 10) from Account limit 10000]){

                   }

system.debug('Limits.getAggregateQueries() '+Limits.getAggregateQueries());
system.debug('Limits.getQueries() '+Limits.getQueries());
system.debug('Limits.getQueryRows() '+Limits.getQueryRows());

Debugs Results are:-enter image description here

Salesforce doesn't count subqueries against governor limit of 100 SOQLs. It counts only root query which means only 1 SOQL query would be consumed.

Subqueries are counted separately as AggregateQueries. You can check those with Limits.getAggregateQueries() and Limits.getLimitAggregateQueries(). You cannot have more than 300 aggregations in a single transaction. This is stated just below the sentence you quoted:

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

| improve this answer | |
  • Thanks for the response. so when ever using SOQL for loop, child queries row count get an exempt from overall row count? In both cases child queries are treated as aggregate queries, So in the second query with for loop, where does the child row count accumulated else where? Will it be added to aggregate row limits or any – SFDC_Igle May 30 '19 at 13:29

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.