Monday, 29 July 2019

The SOQL Count() Function Doesn’t Count (As Much) Toward Limits

Hi,

Previously, each individual record matched by the SOQL COUNT() and COUNT(fieldName) functions counted toward the query row limit. So requesting a count could have a significant impact on governor limits, especially when working with many records. Now, if a query using one of these functions returns an integer, it only counts as one query row toward the governor limit. If a query using one of these functions returns an array of AggregateResult objects, only the total number of AggregateResult objects counts toward the limit.

For example, consider the following query.
Integer countOfContacts = [SELECT COUNT() FROM Contact WHERE Account.Name = 'Edge Communications'];
Previously, the number of records matched by this query counted toward the limit. Now this query counts as only one query row toward the limit.
The following query uses the COUNT(fieldName) function. Previously, the number of rows returned counted toward the limit, but now this query counts as a single query row.
AggregateResult ar = [SELECT COUNT(AccountId) rowcount FROM Contact]; // Count contacts with an account only
Integer rowCount = (Integer)ar.get('rowcount');
If a query that uses the COUNT(fieldName) function contains a GROUP BY clause, only the number of resulting AggregateResult objects count toward the limit. For example, in the query in the following example, only one item per aggregated result is counted toward the limit.
List<AggregateResult> res = [SELECT COUNT(id) FROM Contact GROUP BY AccountId];
System.assertEquals(res.size(), Limits.getQueryRows());
Previously, all the records matched by this query counted toward the query row limit.


Reference:

What’s the difference between Einstein Article Recommendations and Suggested Articles?

How Does Einstein Article Recommendations Work? Einstein Article Recommendations helps support agents resolve customer cases efficiently by ...