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

Too many queries rows 500001


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,
Sagar PareekSagar Pareek
Your organization have very large data set, you need to apply filters in your query.
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]

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

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.
Sagar PareekSagar Pareek
Working with SOQL Aggregate Functions will help you ---
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...

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

For more information, please find it in the link:

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