0

As per the article Execution Governors and Limits

In a SOQL query with parent-child relationship subqueries, each parent-child relationship counts as an extra query. The row counts from these relationship queries contribute to the row counts of the overall code execution.


I have run the following code

for (Account acc : [
        SELECT (SELECT Id FROM Orders), (SELECT Id FROM Opportunities), (SELECT Id FROM Contacts)
        FROM Account
]) {
    // to something
}

Below the governor limits overview after the run:

  • Number of SOQL queries: 1 out of 100
  • Number of query rows: 32512 out of 50000 ******* CLOSE TO LIMIT

  • Number of aggregate queries 3 out of 300

I have in total 32512 accounts on salesfroce which matches the number of fetched rows, the number of SOQL queries is one and it consumed 3 aggregate queries.


Questions

  • Why the number of queries used and rows fetch did not increase? The number should be higher since I am using aggregate queries
  • What they mean with "These types of queries have a limit of three times the number for top-level queries" (Article above)?
1

The 32512 row count should include all of your accounts that you queried, plus all of the Orders, Opportunities, and Contacts. I suspect that if you would store the query in a list an debug list.size(), that it would not be 32512. You could also declare an integer outside your loop and increment it on each run through your loop.

Your second question is pretty simple too. For "top-level" queries, i.e. [SELECT <fields> FROM <SObject>], we can perform 100 queries in a synchronous transaction.

For inner queries (called "Aggregate queries" in the debug log), e.g. the (SELECT Id FROM Orders) part of your given query, we can perform 300 of them in a synchronous transaction.

Since your query contains 3 inner queries, that's a total of 1 top-level query, and 3 "Aggregate queries", which lines up with what you're seeing for limit usage.

  • I have 32512 accounts I checked the list.size(). This is really weird :(. This means that the aggregation query did not contribute to the number of queries issued and fetched rows. – Cuban coffee Jul 11 '18 at 12:55
  • @Cubancoffee Your aggregate queries did contribute to your governor limits, that's precisely why you see aggregate queries: 3 out of 300 in your logs. As for the rows returned, maybe Salesforce doesn't count child records towards query rows until you actually iterate through the child records. – Derek F Jul 11 '18 at 13:02
  • I agree it contributed but only with the aggregate queries. I also iterate over the child records, same thing :( – Cuban coffee Jul 11 '18 at 13:05

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.