You need to sign in to do that
Don't have an account?
Sanch
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.
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
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!!
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
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.
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.
So for you no need to use Limit in your query :)
Thanks a lot.
Is there a difference between these two set of code?
AND
thanks.
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.
Hi,
You should take it as a reference when you are using SOQL queries
The above stuff is from Apex Language Reference!!