+ Start a Discussion
Ron WildRon Wild 

Too Many Query Rows with Count()


I'm getting the error 'Too Many Query Rows: 1001' when I make the call:

Integer value = [SELECT count() from Contact where .....];

And the debug log confirms that even though count() is an aggregate function, the Apex governor counts every row matching your select criteria.

Apparently from inside a trigger the limit is 1000 and from a webservice call 10000.


Is there any way the count() function can be measured as a single SOQL row for governance purposes?   It seems like it's just a toy function otherwise.


werewolfwerewolf
Depending on what your criteria are, perhaps you could make a rollup summary field from contact to account instead?  Like, if your where somehow includes the account then you can make this field on account with whatever other criteria you want, and then use that as your count.
Ron WildRon Wild
Good idea, but that would require that the number of accounts containing these contacts be significantly smaller than the number of contacts.  In many cases you will have 1-1 or 1-2 relationship between Account and Contacts, so the count() function will again exceed governor limits.

Also, you would need a sum() function to give you the actual count of contacts rolled up to the Account records ... and the SOQL command set doesn't include that function yet.

Regards,


Execute EZSAASExecute EZSAAS
Ron, I am running into the same issue -- have you been able to resolve it? Appreciate if you can share

Ron WildRon Wild
I think Asynchronous calls give higher governor limits, but I don't know how much higer - haven't tried them yet.   This is a feature you have to call Salesforce to turn on in an account.