+ Start a Discussion
Sam.arjSam.arj 

SOQL count() and Too many query rows: 10001!


I am developing a page in which i want to show total number accounts who have certain condition, my code is like this:
Code:
 if (ptotalRecords == 0)
       {
            try
            {
              ptotalRecords = [select count() from Account WHERE (Hierarchy_Status__c = 0 OR Hierarchy_Status__c = null) AND (parent_duns_number__c != null OR Ultimate_Parent_D_B_Number__c != null)];
            }
            catch(Exception ex)
            {
                 ptotalRecords = -1; 
            }
      }
       
      if (ptotalRecords == -1)
         return '10,000+';
      else
         return ptotalRecords+ '';

 Even though i have put everything in try and catch block i still get the following error:

System.Exception: Too many query rows: 10001

it seems as a developer i do not have the right to choose the fate of my App!



A_SmithA_Smith
Try adding "limit 10000" to the end of your query.
Sam.arjSam.arj

In that case how would I know if there were only 10,000 records or more?

BoxBox

Sam.arj wrote:

 Even though i have put everything in try and catch block i still get the following error:

System.Exception: Too many query rows: 10001

it seems as a developer i do not have the right to choose the fate of my App!


I'm afraid as a consumer of the platform once you trigger a governor exception you do not have the ability to catch and perform additional processing.  You have touched on a somewhat sore point where the count() operation is being treated as the number of line items not the single summation returned for the SOQL query limit.  It has been raised on the ideas page and please vote for this behavior to be changed - http://ideas.salesforce.com/article/show/10089055


Message Edited by Box on 07-31-2008 09:27 AM
dke01dke01
The Real WTF  is why isn't COUNT() returning an Interger   instead of a row for every record.
sfdcfoxsfdcfox
SELECT count() does indeed return one row, with an integer value. However, the number of rows counted is tallied against the limits of your current context; you only get one row but you are penalized as if you'd returned all that data manually. This is to prevent excessive resources from being used from one context call. Note that you can use a for(list<xx> x:[select id from xx]) loop, checking Limits.getQueryRows against Limits.getLimitQueryRows and aborting gracefully if you hit this limit; you would know if you had 9999 records, but if you had 10000 or more than 10000, there would be no way to identify this borderline condition. You might consider using Batchable Apex if you absolutely need an answer (note that you'd have to wait for the results to finish).
SidMSidM

So, with the release of the new COUNT(Id) aggregate function, do you think we'll still be limited to a maximum count of 10,000 (due to the fact that the Governor considers COUNT the same as SELECT)??

 

Seems crazy that we can't actually tell how many records exist for a particular SOQL query!

UnknownUserUnknownUser

I have an Object with 80 million Records. I have other objects with a couple million. I can run a report and a dashboard on the Objects under 4 million, but I can't get any reports to run on the Object with 80 million records. When I try to select count() from Object, I get timeout.

 

There needs to be functionality to count records with either SOQL or reports/dashboards. It's the only way to automate operational reports.

 

Any Idea if/when count(Id) will be available, and will it be able to count 80 million records?

 

Thanks. Regards,

Tom