I am checking the governor limits of SOQL queries and just want to confirm few things:
- When we have more than 200 records related to 1 account I am getting exception as : System.QueryException: Aggregate query has too many rows for direct assignment, use FOR loop. Please check below code:
for(Account acc:[Select id,(select id from contacts) from account where id = '0015g000003EgfK']){ System.debug(acc.contacts.size()); }
why .size function is giving issue?
- SOQL query can query upto 50,000 records but what is the limit of child query records (Contacts records) that can be returned?
- If there is a limit of the rows return on child query will it be depending upon the account id like with 1 account only 200 or 300 contacts can be returned?
integer i = 0; for(Account acc:[Select id,(select id from contacts) from account where id = '0015g000003EgfK']){ for(contact con: acc.contacts){ i = i+1; } } system.debug('Total Contacts:'+i); system.debug('Total Aggregate Queries:'+limits.getAggregateQueries()); system.debug('Total Query Rows:'+limits.getQueryRows()); system.debug('Limit query rows:'+limits.getLimitQueryRows());
What limits.getQueryRows() should be returning? Will it be total count of contacts + account records or just total count of account records? Somehow for me its return 13 result while contacts counted by I variable is 211 and only 1 account is present.
If I need to find out Average closing time for all cases related to account it, what could be the approach here as each account can have more than 50,000 cases so I cant use aggregate Result sum function while grouping it by accountid. I am looking for a trigger based approach rather than batch.
@derekF, Let me try to summarize and ask a few more detail on what you just posted above.
- if you have more child records than can be returned in a single chunk, you won't be able to use .size() --- Correct, I just tried in my org, although the solution I used is different. Below works for me, here total contacts are 2132 related to one account.
List<Account> accList = [Select id,(select id from contacts) from account where id = '0015g000003EgfK']; system.debug('accList size' + accList[0].contacts.size()); for(Account acc:accList){ system.debug('Acc related Contacts size' + acc.contacts.size()); for(contact con: acc.contacts){ i = i+1; } }
- The 50k limit for query rows returned is for the entire transaction. There is no separate limit for parent-child subqueries --- I tried same code now in full copy sandbox and below are the results. Why the total query rows still showing 30,000?
Rows queried as part of Semi-Joins and Anti-Joins do not count towards the query row limit. --- The joint query still be enforcing 50,000 rows governor limit right?
There is no strict limit on the number of child rows that a parent-child subquery can return (outside of the overall 50k row governor limit that we're subject to) --- I know it will surely index skew issues when you have that much records. I was testing how many child records we can get when we do parent to child query. Coming to your answer, does that means there no limit on the number returned for contacts in parent to child query mentioned above right? 50,000 will only be limit for accounts?