[SalesForce] How Aggregate Result are count against the Governor Limits

I'm hitting the Governor limits (Too many query rows: 50001) and I would like to improve my query. However is only an Aggregate Result that only brings 30 rows but it reads a lot because is against the Invoices table. I'm wondering if Aggregate results are count by the amount of rows that the query brings or against the row it reads?

Is just a simple query and I don't see anything wrong on

AggregateResult[] LastYTD_invoicevalue =[Select Sold_To__r.Class_Code__c c,Sum(Invoice_Amount__c)s from Invoice_New__c where (Effective_Date__c >=2015-07-01 and Effective_Date__c <=2016-10-23) Group By Sold_To__r.Class_Code__c Limit 1000];

enter image description here
I do 4 querys (MTD, Last MTD, YTD and Last YTD) like the one above and put them in 4 different MAPS and the read the 4 of them to display the results.

Why is this query is only bringing 30 rows each one am I hitting the limits?

Thanks in advance.

Best Answer

The number of queried records is what is counted against you. Even if you only get one AggregateResult row back, if that row is tallied from 50,000 records, then you'll exceed the governor limit. You'll find documentation that states that in Working with SOQL Aggregate Functions.

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.

Related Topic