I'm repeatedly getting following known exception:

Too many query rows: 50001 
An unexpected error has occurred. Your development organization has been notified.

The thing here is that my query is retrieving only 18 rows, so my jaw fall to the floor instantly.

This is my query:

SELECT Date__c, Country__c FROM Insight2__c GROUP BY Date__c, Country__c ORDER BY Date__c ASC

This are the log main lines, check the 2nd line to verify there is only 18 rows retrieved:

16:24:46.0 (110288675)|SOQL_EXECUTE_BEGIN|[6]|Aggregations:0|SELECT Date__c, Country__c FROM Insight2__c GROUP BY Date__c, Country__c ORDER BY Date__c ASC NULLS FIRST
16:24:46.0 (380090845)|SOQL_EXECUTE_END|[6]|Rows:18
16:24:46.0 (380142615)|EXCEPTION_THROWN|[6]|System.LimitException: Too many query rows: 50001
16:24:46.0 (380413663)|HEAP_ALLOCATE|[6]|Bytes:30

This is the screenshot of the developer console query editor with the exact same query and the above mentioned 18 rows:

enter image description here

This is my code:

public static List<DateInfo> GetStruct() {      
    Map<String, DateInfo> temp = new Map<String, DateInfo>();

    List<AggregateResult> insights = [  SELECT 
                                        FROM Insight2__c
                                        GROUP BY 
                                        ORDER BY Date__c];

    for(AggregateResult inv: insights){
        Date d = (Date)inv.get('Date__c');
        String country = (String)inv.get('Country__c');

        if(temp.get(country) == null) {
            Set<String> months = new Set<String>();
            months = GetPeriodsByDate(d, months);

            DateInfo dInfo = new DateInfo(country);
            dInfo.Add(d.year(), months);

            temp.put(country, dInfo);
        else {
            DateInfo dInfo = temp.get(country);

            Set<String> months = dInfo.GetInfo(d.year());

            if(months == null)
                months = new Set<String>();

            months = GetPeriodsByDate(d, months);

            dInfo.Add(d.year(), months);

    return temp.values();

Thanks in advance!

  • Is the class in which GetStruct() is defined "with sharing" or "without sharing"? Apr 6, 2016 at 21:15
  • Hi, class is declared "with sharing"
    – Marcel
    Apr 6, 2016 at 21:17

2 Answers 2


In aggregate queries, all of the records that produced the aggregate result count against you, even though only a few "rows" are actually returned. If you have more than 50,000 rows of data in your Insight2__c object, you'll get this exception. Also note that 50,000 rows is the total for the entire transaction. You can just as easily generate this error by doing something like this:

Contact[] contact1 = [SELECT Id FROM Contact LIMIT 49999];
Account[] account1 = [SELECT Id FROM Account LIMIT 2];

For example, I can run this code in my developer org that has almost no data:

AggregateResult[] results = [select ownerid from account group by ownerid];
System.debug(LoggingLevel.ERROR, results.size());

And the logs will show:

15:20:23:000 LIMIT_USAGE_FOR_NS   Number of SOQL queries: 1 out of 100
15:20:23:000 LIMIT_USAGE_FOR_NS   Number of query rows: 16 out of 50000

Even though the query returned these values:

15:20:23:009 USER_DEBUG [2]|ERROR|2
  • Hi sfdcfox, thanks for your interesting replay. I've never imagined ungroupped records will count for limits, salesforce is killing me. So, what can I do to manage large amounts of data taking into accounts all records (meanning no LIMIT clause but GROUP BY) and perform statistics calculations??
    – Marcel
    Apr 6, 2016 at 21:24
  • @Marcel You can use the read-only property of Visualforce. This gives you 1,000,000 rows.
    – sfdcfox
    Apr 6, 2016 at 21:28
  • I'm going to try that, seems to be the closest way to achieve my goal. Thanks a lot @sfdcfox!
    – Marcel
    Apr 6, 2016 at 21:30

The total number of rows which were accessed on the number of AggregatedRows are returned so if 1 of your AggregatedRows touches more than 1 more you hit the limit

Queries that include aggregate functions are subject to the same governor limits as other SOQL queries for the total number of records returned. This limit includes any records included in the aggregation, not just the number of rows returned by the query. If you encounter this limit, you should add a condition to the WHERE clause to reduce the amount of records processed by the query.


  • Thanks EricSSH, I'm still wondering how to perform calculations over the entire data and avoid limit exceptions. Is salesforce ready to large amounts of data?
    – Marcel
    Apr 6, 2016 at 21:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .