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
VSK98VSK98 

Too many queries rows 500001

Hi,

How to avoid Too many queries rows 500001 in the apex class.

I have one query 
list<Account> acc = [select id,Fax from Account];

As per requirement i dont need to use where condition in the above query .............

Any suggestions,
Regards,
VSK
Sagar PareekSagar Pareek
Your organization have very large data set, you need to apply filters in your query.
VSK98VSK98
Agreed Sagar, But i dont wanna add filter condition in the above query.

Is there any alternative ?
Shukla YogeshShukla Yogesh

Hi VSK98,

You should use LIMIT to control the governor limit of 50000 records.

Use below syntax:

 

SELECT ***fieldList***
FROM objectType
[WHERE conditionExpression] 
  [LIMIT numberOfRows]
VSK98VSK98
Yogesh,

I would like to use all my account records & do sum action on it.

Regards,
VSK
Shukla YogeshShukla Yogesh
You should look at using Batch Apex as you cannot retrieve more than 50,000 records your SOQL calls in a single context.
However, with  Batch Apex your logic will be processed in chunks of anywhere from 1 to 200 records in a batch.
 
You'd need to modify your business logic to take the batching into account if necessary.
 
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_batch_interface.htm
Sagar PareekSagar Pareek
Working with SOQL Aggregate Functions will help you ---
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SOQL_agg_fns.htm
Ray C. KaoRay C. Kao
There are two ways to fetch more than 50,000 rows in the Apex.
(1) Batch apex
(2) Readonly mode

If the code is used in a readonly report, then please consider to add the Readonly annotation in the code.

Here is an example:
Visualforce page
<apex:page controller="LargeDataVolumn" readOnly="true">
    Report program...
</apex:page>

Alex Class
public class LargeDataVolumn{
    public Integer getNumberOfAccounts() {
        return [ SELECT COUNT() FROM Account ];
    }
}

For more information, please find it in the link:
https://developer.salesforce.com/docs/atlas.en-us.200.0.pages.meta/pages/pages_controller_readonly_context_pagelevel.htm

BTW, aggregate SOQL does not skip away the query row limitation.