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
SanchSanch 

How does the Limit Keyword affect the result in a for loop?

Hi All,

 

I have the following code

 

 

for(Contact[] contList : Database.query(queryToExecute + 'limit 10000')) {
	for(Contact cont : contList) { }
}

 

When I do this, how does the Limit keyword affect the result? Let's assume there is 30,000 records that match the query.  I have a limit of 10,000 in the query. Would it pull the 10,000 only and forget the rest? OR would it pull the 10,000 first process them and then pull the next 10,000 and process that and the next 10,000 and process them? How does this work? Thanks.

 

Sanch.

 

Best Answer chosen by Admin (Salesforce Developers) 
SuperfellSuperfell

limit controls the total number of rows returned, how you subsequently access those rows makes no difference. Its not doing any paging. In the case where you using the array accessor, then those 10k rows are broken up into chunks for the loop (probably of 100 or 200 rows each chunk), but after the loop has finished, you only processed 10k rows total.

All Answers

MandyKoolMandyKool

Hi,

 

With this syntax of "for" loop, it will query 10,000 records in first go.

Will process those 10000 records (i.e. It will proceeds through the for loop).

Again in next go it will query next 10000 records and in that way it will proceed.

 

I think internally it uses the QueryLocator object and will keep track on how many records are queried yet. And in next go it will query next set of records.

 

Hope this will help!!

SanchSanch

So the Limit keyword is not associated with the query result, it is actually associated with the querylocator? Because normally if you say limit 10,000, my understanding was that it will only return the first 10,000 records that match the criteria. But what you are saying is different, which is what I want. Can you confirm that? Thanks.

 

Sanch

SuperfellSuperfell

limit controls the total number of rows returned, how you subsequently access those rows makes no difference. Its not doing any paging. In the case where you using the array accessor, then those 10k rows are broken up into chunks for the loop (probably of 100 or 200 rows each chunk), but after the loop has finished, you only processed 10k rows total.

This was selected as the best answer
MandyKoolMandyKool

Hi, 

 

Sorry for wrong info.

 

When you are using for(List<sObject> :[Query]) this syntax actually you will not require to use Limit in query.

If you use "Limit" definately it will query only those many records.

 

If you want you can confirm this using following code snippet.

 

integer iCount = 0;
for(List<Account> lstAcc : [Select Id,Name from Account])//Use Limit here and check.
{
 for(Account objAcc : lstAcc)
{
 system.debug('Account Name::' + objAcc.Name);
 system.debug('Count::' + ++iCount);
}
}

 

 So for you no need to use Limit in your query :)

 

SanchSanch

Thanks a lot.

SanchSanch

Is there a difference between these two set of code?

 

 

integer iCount = 0;
for(List<Account> lstAcc : [Select Id,Name from Account])//Use Limit here and check.
{
 for(Account objAcc : lstAcc)
{
 system.debug('Account Name::' + objAcc.Name);
 system.debug('Count::' + ++iCount);
}
}

 

 

AND

 

 

integer iCount = 0;
for(Account objAcc : [Select Id,Name from Account])//Use Limit here and check.
{
 system.debug('Account Name::' + objAcc.Name);
 system.debug('Count::' + ++iCount);
}

 thanks.

 

sfdcfoxsfdcfox

There's no difference between the two as far as the query is concerned. When you perform a query, the database targets all records in the database that match your filters, sort it by the order and group operators, then returns the list of items less than or equal to the limit you specify. The database only counts returned rows for governor limits.

MandyKoolMandyKool

Hi,

 

You should take it as a reference when you are using SOQL queries

 

 

// Use this format if you are not executing DML statements
// within the for loop
for (Account a : [SELECT id, name FROM account
WHERE name LIKE 'Acme']) {
// Your code without DML statements here
}
// Use this format for efficiency if you are executing DML statements
// within the for loop
for (List<Account> accts : [SELECT id, name FROM account
WHERE name LIKE 'Acme']) {
// Your code here
update accts;
}

 The above stuff is from Apex Language Reference!!