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
kpetersonkpeterson 

Query Rows Are Being Counted Wrong?

I've had "Too Many Query Rows" come up quite a few times now and i have not gotten any official response so I've made some simple cases to show the error.

I have a Full sandbox from production.  If I run this query in the explorer I get a count of 8987.

Code:
select count() from opportunitylineitem

I've created this test method to test my different limit issues.

Code:
public class CountTests {

 static testMethod void testCountTests()
 {
  Integer iCount = 0;
  for (OpportunityLineItem oOpportunityLineItem : [SELECT Id FROM OpportunityLineItem])
  {
   iCount++;
  }
  system.debug(iCount);
  
  system.debug([SELECT COUNT() FROM OpportunityLineItem]);
 }
}

The first section (the for loop) fails with "Too many query rows: 501" but it was my understanding that I should be able to process more records than the limit through the SOQL For Loop setup.  Does the SOQL For Loop still count towards the Query Rows count?

The second section (the count SOQL) also throws a "Too many query rows: 501" but I'm not returning the rows.

Could someone please let me know if I'm misunderstanding the limits or let me know if this is a known issue that has a fix/is being fixed.


mtbclimbermtbclimber
Select Count() is bound by the SOQL query rows limit the same way SOQL queries returning rows/columns are.

You are still bound to this limit when using array processing as well. The for loop structure in your test is merely a convenience. In a context where the soql row limit is higher than the max array size - such as webservice methods or visualforce page controller classes/extensions - this array processing will allow you to overcome the 1000 max collection size limit by chunking:

Code:
for (OpportunityLineItem[] oOpportunityLineItems : [SELECT Id FROM OpportunityLineItem])

 




Message Edited by mtbclimber on 02-24-2008 10:13 AM
kpetersonkpeterson
Thank you for the explanation!
mfcmfc
Hi,

I didn't understand. How do I get a count of how many rows are in a table if there
are more than 8k?

Thanks,

Mike
Ron HessRon Hess
I don't think that you can, count() is limited by the same DML limits as SOQL.
mfcmfc
that seems like a missing feature.
mtbclimbermtbclimber
Do you want a raw count of all records or do you want to filter it (and/or apply sharing - enforce visibility restrictions)?
Abhinav GuptaAbhinav Gupta

The key to get this error fixed is ISOLATION of your test data from the org's data. Try executing your test via System.runAs(<User>). I am sure it will help.

This blog post explains this in more detail : http://www.tgerm.com/2010/05/systemrunas-501-apex-query-rows.html