function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
HardikHardik 

SOQL count() returns incorrect value

In my application am using a custom object. When I try to fetch the count using "select count()..." it throws the exception - Too many query rows. Whereas Count aggregated function returns me the count 10226 and the same exception, but the custom object has only 660 records.

 

Is it a bug or am missing something?

Best Answer chosen by Admin (Salesforce Developers) 
Going South.ax738Going South.ax738

Yes, i believe its the problem. You might need to change entire program logic so as to not to allow any sql inside any loop whatsover. Recommends to define map and list items at the top of class, do just one SOQL fetch and use it for rest of program logic by querying on that map/list for processing data into other maps & lists.

 

see this link on governor limits.

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_gov_limits.htm

All Answers

WesNolte__cWesNolte__c

Hey

 

Is there a nested query in the main query? Could you post the code please.

 

Wes

 

HardikHardik

This is the select query:

Integer total = [select count() from MyActivity__c where Activity_Id__c = null and CreatedBy.UserName =: UserInfo.getUserName()];

 

Actually this issue occur when there are multiple select operations in a single thread. Its like I query some other custom object and in the same thread I also query this custom object on click of a link in the UI. But it works when I invoke only this query separately.

 

 

WesNolte__cWesNolte__c

So your controller code has more than one select statement? Try using Limit.getDMLRows class in conjunction with System.debug messages before and after your queries. This will show you how and where you're building up DML rows. Check out the Limit class docs here: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_limits.htm

 

Wes

HardikHardik
Tried using Limit.getDMLRows class in conjunction with System.debug.
Before and after queries displays Limit as 2 and 5.
WesNolte__cWesNolte__c

Sorry I must've been half asleep, you should be using the Limit.getQueryRows() class.

Foxy JoelFoxy Joel

You may be running into the default limits on queries which do add up per invocation.

 

Have you tried explicitly setting the limit on the query     [ select  fields from table where condition      limit 14000 ]

 

Joel Schaubert

 

Going South.ax738Going South.ax738

Suggests to run the query outside as-is in Eclipse scema to see if the query is valid and check on limit. ofcourse, hardcode the username that you are trying to find.

 

My guess is you have ran out of limit on your apex soql limits as you might have some soql within loop. Comment all other parts of your apex to see if it run fine.

Also, check on debugger/system log on what is the running count at the end of log listings.

 

HardikHardik

In a single thread I've multple database calls, is the limit 10K is for total records fetched in a single thread execution or it is based on an indivisual query. As this count keeps growing with the number of Select queries executed in the same thread?

 

Am actually using the aggregated function MAx multiple times on the custom object with 640 records, If this is how it is, the count will go to 15 * 640 only for the MAX itself and maybe thats the reason its failing.

 

Putting limit to query will not help as any single query will return maximum 640 records.

Going South.ax738Going South.ax738

Yes, i believe its the problem. You might need to change entire program logic so as to not to allow any sql inside any loop whatsover. Recommends to define map and list items at the top of class, do just one SOQL fetch and use it for rest of program logic by querying on that map/list for processing data into other maps & lists.

 

see this link on governor limits.

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_gov_limits.htm

This was selected as the best answer
HardikHardik

You were right, query rows count is thread based. Now am updating my logic accordingly.

 

Thank you all for your support and the useful links on Salesforce..