8

Per the Limits class documentation, there are a pair of methods for "aggregate queries":

getAggregateQueries()

Returns the number of aggregate queries that have been processed with any SOQL query statement.

getLimitAggregateQueries()

Returns the total number of aggregate queries that can be processed with SOQL query statements.

To what limit do these methods refer? I have checked the Limits Guide, and I can't find any limits specific to aggregate queries. In my DE org, Limit.getLimitAggregateQueries() returns 300; I can find no relevant limit with a value of 300. I've used these methods in measuring code that runs SOQL aggregate queries (albeit dynamic queries, via Database.query()); Limit.getAggregateQueries() always returns 0, and the queries are always counted by Limit.getQueries(). Does this refer to some limit no longer in use? Is it documented elsewhere?

7

Possibly the naming AggregateQueries is very misleading. My first thought was it should be related to a GROUP BY clause - but it seems to be different. I did not try it on my own but found this article could help with the clarification:

https://bartoszborowiec.wordpress.com/2014/12/12/soql-subqueries-and-limits/

Following this article, the limit seems to count subselects at the SELECT clause

        select name,
            ( select id, name, Contact__r.Name
            from AccountContacts__r
            where name != null  )
        from Account           

but not in the WHERE clause

        select name            
        from Account
        where id in ( select Account__c from AccountContact__c )

Try the complete code

@isTest
private class SubqueriesTest {

    @isTest
    static void testQueriesAgainstLimits() {
        System.assertEquals( 0, Limits.getAggregateQueries() );
        System.assertEquals( 0, Limits.getQueries() );
        List<Account> accounts = [
            select name            
            from Account           
        ];
        System.assertEquals( 0, Limits.getAggregateQueries() );
        System.assertEquals( 1, Limits.getQueries() );
    }

    @isTest
    static void testSubqueriesInWhereClauseAgainsLimits() {
        System.assertEquals( 0, Limits.getAggregateQueries() );
        System.assertEquals( 0, Limits.getQueries() );
        List<Account> accounts = [
            select name            
            from Account
            where id in ( select Account__c from AccountContact__c )
        ];
        System.assertEquals( 0, Limits.getAggregateQueries() );
        System.assertEquals( 1, Limits.getQueries() );
    }

    @isTest
    static void testSubqueriesInSelectClauseAgainsLimits() {
        System.assertEquals( 0, Limits.getAggregateQueries() );
        System.assertEquals( 0, Limits.getQueries() );
        List<Account> accounts = [
            select name,
                ( select id, name, Contact__r.Name
                from AccountContacts__r
                where name != null  )
            from Account           
        ];
        System.assertEquals( 1, Limits.getAggregateQueries() );
        System.assertEquals( 1, Limits.getQueries() );
    }          

    @isTest
    static void testSubqueriesAgainsLimits() {
        System.assertEquals( 0, Limits.getAggregateQueries() );
        System.assertEquals( 0, Limits.getQueries() );
        List<Account> accounts = [
            select name,
                ( select id, name, Contact__r.Name
                from AccountContacts__r
                where name != null  )
            from Account
            where id in (select Account__c from AccountContact__c)
        ];
        System.assertEquals( 1, Limits.getAggregateQueries() );
        System.assertEquals( 1, Limits.getQueries() ) ;
    }  

} 
  • Aha! And now that I know what to search for, I found this in a footnote on the Total Number of SOQL Queries governor limit (emphasis added): "In a SOQL query with parent-child relationship sub-queries, each parent-child relationship counts as an additional query. These types of queries have a limit of three times the number for top-level queries. The row counts from these relationship queries contribute to the row counts of the overall code execution." Since the synchronous limit on number of queries is 100, the 300 limit I saw makes total sense. And +1 for "very misleading" – Jason Clark Feb 10 '15 at 17:09
  • I was thinking of that myself. Glad we have some documentation on that. I even checked the limits documentation, but it didn't register with me. – sfdcfox Feb 12 '15 at 22:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.