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
Marc FriedmanMarc Friedman 

I am receiving a Too many query rows: 50001 error but the debug log does not show where

We're receiving an error stating that we have too many query rows, but the debug log is not showing us where these are coming from. Below is each line in the debug log, in order, containing "SOQL_EXECUTE_END" and its corresponding SQL statement:

11:05:55.603 (1603125374)|SOQL_EXECUTE_BEGIN|[27]|Aggregations:0|select AccountId, SUM(Amount) total from Opportunity where (IsWon = true and Rollup_Stop_Date__c > :tmpVar1 and AccountId = :tmpVar2) group by AccountId
11:05:58.148 (4148435199)|SOQL_EXECUTE_END|[27]|Rows:292
11:05:58.149 (4149429111)|SOQL_EXECUTE_BEGIN|[30]|Aggregations:0|select Id, ParentId, TTM_Sales__c from Account where Id = :tmpVar1
11:05:58.185 (4185336466)|SOQL_EXECUTE_END|[30]|Rows:293
...
11:06:00.810 (6810750395)|SOQL_EXECUTE_BEGIN|[18]|Aggregations:0|select Id, AccountId, Account_Level_Expire__c, Account_Level__c, Hierarchy_TTM_Sales__c, CompanyName from User where AccountId IN :tmpVar1
11:06:00.906 (6906647173)|SOQL_EXECUTE_END|[18]|Rows:365
...
11:06:03.387 (9387092122)|SOQL_EXECUTE_BEGIN|[114]|Aggregations:0|select ParentId, SUM(TTM_Sales__c) total from Account where ParentId = :tmpVar1 group by ParentId
11:06:03.412 (9412617167)|SOQL_EXECUTE_END|[114]|Rows:14
11:06:03.412 (9412951386)|SOQL_EXECUTE_BEGIN|[117]|Aggregations:0|select Id, Hierarchy_TTM_Sales__c, ParentId, TTM_Sales__c from Account where Id = :tmpVar1
11:06:03.447 (9447288861)|SOQL_EXECUTE_END|[117]|Rows:301
11:06:03.489 (9489047107)|SOQL_EXECUTE_BEGIN|[151]|Aggregations:0|select Id, ParentId, TTM_Sales__c from Account
11:06:03.509 (9509692382)|SOQL_EXECUTE_END|[151]|Rows:140
...
Number of query rows: 30222 out of 50000 ******* CLOSE TO LIMIT

So, this is showing that it selecting a total of 1405 rows, then all of the sudden it is at 30222 without ever having shown that it selected anywhere near this many records. Thoughts?

Thanks,
Marc
John De SantiagoJohn De Santiago
I see that you are using aggregate queries and unfortunalely the limits apply to all records in the aggregate and not just the number of returned rows. So if you sum across 500 records that only returns 1 record. The counter will still count that as 500 records towards your query limit. 

See the note at the bottom of the documentation:
http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_SOQL_agg_fns.htm