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
DonSpilkyDonSpilky 

SOQL FOR loop and 500/10,000 row limit

I had thought that using a SOQL FOR loop would solve the problem around retrieving too many rows from a query, because it would return 200 at a time (implicit query/querymore).

 

However, I am getting the "too many rows back" error anyways, on the actual [soql-query] portion of the FOR loop.

 

Are we really limited to 500/10,000 rows back even on the [soql-query] term? If so, does anyone have a workaround to iterate through really large data sets?

 

We can use:

for (Account a : [Select id from account]){

      …

}

 

as the example query.  Assuming I have > 500/10,000 accounts, how I can iterate through them all?

 

Thanks!

Don

Best Answer chosen by Admin (Salesforce Developers) 
DonSpilkyDonSpilky

Bottom line, it seems that there is no way around the 10,000 sql row limit at this time.

 

Work around will have to be done to run 2 queries, probably with a “where” clause on names “a-m” “M-z” or something like that, then combine the results.

All Answers

Imran MohammedImran Mohammed

SOQL query can return upto 10000 records. So this is limitation if you have to process large data sets.

Use Batch Apex that can process upto 50 million records in a single query.

You can find more info on batch apex in the Apex documentation.

 

if you have any further queries post them

 

DonSpilkyDonSpilky

Batch isn't going to help me here, because I need to iterate through this data set when its called as a webservice.  The iteration needs to be real-time.

forecast_is_cloudyforecast_is_cloudy

If you use the following syntax, you can only retrieve upto 1000 rows - any more and you'll get a runtime error:

 

for (Account a : [Select id from  account]){



}

 

If you use the following syntax however, you can retrieve upto 10,000 recods, in batches in 200 (the implicit query more pattern).

 

for (List<Account> accts : [Select id from  account]){
//The accts list will have 200 records at a time
for (Account a : accts)
{

}
}
DonSpilkyDonSpilky

Bottom line, it seems that there is no way around the 10,000 sql row limit at this time.

 

Work around will have to be done to run 2 queries, probably with a “where” clause on names “a-m” “M-z” or something like that, then combine the results.

This was selected as the best answer