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
FaridKognozFaridKognoz 

Count more than 10000 records

Hi;

I want to use count() on an soql query. When there are more than 10.000 records, an apex exception is trown "To many query rows". Is it not possible to count more than 10.000 records?

Thanks,

 

jhenningjhenning
Looks like you have hit the governor limit for SOQL records. http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_gov_limits.htm
ThomasTTThomasTT

As jhenning says, it's about governor  limit.

However, if you really want to count them at any cost, there are some workarounds... by using Apex Batch or VF page. The point is that you are allowed to read only 10,000 records in "1 REQUEST", so you just count only 10,000 (or 1,000) records in 1 request, but make multiple requests untill you count everything (and you add them up).

 

That takes you much effort and the process itself takes a long time, but sometime, like for maintenance application, you need to do it. However, if it's not something like that and you have a choice to change the requirement, you should consider the requirement itself. The governor limit is not our enemy, but our friend. It's just telling you that SFDC is not for something that we usually do with Java or bach application.

 

If you can't imagine how you implement the workarounds, I recommend you shouldn't take that risk.

 

Regards,


ThomasTT

VarunCVarunC

ThomasTT wrote: 
The governor limit is not our enemy, but our friend. It's just telling you that SFDC is not for something that we usually do with Java or bach application.

 

If you can't imagine how you implement the workarounds, I recommend you shouldn't take that risk.

 

Regards,


ThomasTT


I totally agree to your point but salesforce also must understand that, whats the logic of having Count() in SOQL query then ? :) ... There has been an Idea Post for Removing the unpleasant thing of Executing Count() like a single row .. but there hasn't been much improvement for making it Actually count not execute and consume valuable Rowsthat Hit the Governor Limits ..

 

User could have averted the situation of reading more than 10000  records if it is Known to user In actual state How Many records are there in the System. but seems like we are at a dead end when dealing with detecting and processing more than 10,000 records. :( ...

ThomasTTThomasTT

I agree with you. Not only count(), we definetely need "group by" itself.

 

However, it is also easy to guess why it is so difficult for them. They basically don't want us to handle or even to assume more than 10,000 records in online application. Their resource, especially database resource, is limited. The result of count() can be indexed in Oracle, but they can't put indexes to all fields for expected where clause.

 

And I kinda agree to SFDC that assuming 10,000 records in online application is not a good design. I can hear the message from the release of "Bach Apex" funciton. If basically SFDC can't handle more than 10,000 records in 1 online request, why user needs to know the exact count rather than "more than 10,000"?

 

Anyway, I guess this is how we live with SFDC, which is very ok to me (this makes me valuable!)

 

ThomasTT

 

VarunCVarunC
I Aggree :) ... but it still remains a debatable topic ;) ...
ThomasTTThomasTT

True. Good discussion! Let's keep posting ideas to Ideas (and vote!).

ThomasTT

mat_tone_84mat_tone_84

hello,

without create a new post I use this.

 

My problem is that I have to do a query into lead but I have more than 10000 leads to process.

 

I create a trigger to find duplicate by filtering with one field , this is the code :

 

 

trigger duplicate_lead on Lead (before update) { if (l.email!= null) { string email= l.email; integer n_email= [Select count () From lead WHERE email =:email ]; if (n_email > 1){ l.maybe_duplicate__c = true; } } }

 I try to use list but doesn't work too.

How can I resolve it?

thanks