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
Marcel GómezMarcel Gómez 

Weird limit exception retrieving 18 rows :?

Hey!
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 is 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:
User-added image

Thanks guys!

 
Naval Sharma4Naval Sharma4
Hi Marcel,

Could you please post your code here?
Marcel GómezMarcel Gómez
Hi Naval,
This is my code, methods called from here do not perform any query at all, just calculations:
public static List<DateInfo> GetStruct() {		
		Map<String, DateInfo> temp = new Map<String, DateInfo>();

		List<AggregateResult> insights = [	SELECT 
												Date__c,
												Country__c
											FROM Insight2__c
											GROUP BY 
												Date__c,
												Country__c
											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
JeffreyStevensJeffreyStevens
Pretty sure - even when you do an Aggregrate Result - it will retrieve all of the records as specified by the WHERE clause.  In this case - you don't have a WHERE clause, so insight__2 must have more than 50k records in it.
Naval Sharma4Naval Sharma4
Hi Marcel,

If you want to handle this situation then go for Apex Batch there you can query 50M records.

Regards,
Naval