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
phoenix7788phoenix7788 

Too many query rows: 50001

hi

I have a apex batch:

  global Iterable<AggregateResult> start(Database.BatchableContext bc) {
    AggregateResult[] sg= [SELECT Phone FROM Account WHERE Phone <> null GROUP BY Phone HAVING COUNT(Phone) >= 2];
    return sg;
  }

but it doesnt working.The error is:

09:39:03.0 (22546931)|SOQL_EXECUTE_BEGIN|[32]|Aggregations:0|SELECT Phone FRO Account WHERE Phone != NULL GROUP BY Phone HAVING COUNT(Phone) >= 2
09:39:03.0 (570162894)|SOQL_EXECUTE_END|[32]|Rows:44
09:39:03.0 (570212289)|EXCEPTION_THROWN|[32]|System.LimitException: Too many query rows: 50001
09:39:03.0 (570386384)|HEAP_ALLOCATE|[32]|Bytes:30
09:39:03.0 (570430741)|SYSTEM_MODE_EXIT|false 09:39:03.0 (570514055)|FATAL_ERROR|System.LimitException: Too many query rows: 50001

i dont know why the error is happen.
(there is no error by using Name field
× System.LimitException: Too many query rows: 50001
AggregateResult[] sg= [SELECT Phone FROM Account WHERE Phone <> null GROUP BY Phone HAVING COUNT(Phone) >= 2];
○ No error
AggregateResult[] sg= [SELECT Name FROM Account WHERE Name <> null GROUP BY Name HAVING COUNT(Name) >= 2];
)

Is there a way to solve this?
 
JyothsnaJyothsna (Salesforce Developers) 
Hi,
  • You can use the Database.executeBatch(sObject className, Integer scopeSize) method to set the batch size.
  • Used Limit and WHERE CLAUSES
  • Also, since you are writing Batch Code, you can also try reducing your scope. The standard batch size is 200 records, but you can increase this up to 2,000 or reduce it to, say, 50. If you are querying a lot of records in the Execute method, it might help to reduce the batch size so the total # records queried per batch is < 50,000.

Hope this helps you!
Best Regards,
Jyothsna