SOQL for loops differ from standard SOQL statements because of the method they use to retrieve sObjects. While the standard queries discussed in SOQL and SOSL Queries can retrieve either the count of a query or a number of object records, SOQL for loops retrieve all sObjects, using efficient chunking with calls to the query and queryMore methods of the SOAP API. Developers should always use a SOQL for loop to process query results that return many records, to avoid the limit on heap size. Please refer : http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_loops_for_SOQL.htm
Since Apex runs in a multitenant environment, the Apex run time engine strictly enforces a number of limits to ensure that runaway Apex does not monopolize shared resources. These limits, or governors, track and enforce the statistics outlined in the following table. If some Apex code ever exceeds a limit, the associated governor issues a run time exception that cannot be handled.
Governor Limits in Salesforce :
Total number of SOQL queries issued---100
Total number of SOQL queries issued for Batch Apex and future methods--200
Total number of records retrieved by SOQL queries--50,000
Total number of records retrieved by Database.getQueryLocator--10,000
Total number of SOSL queries issued--20
Total number of records retrieved by a single SOSL query---200
Total number of DML statements issued-----150
Total number of records processed as a result of DML statements, Approval.process, or database.emptyRecycleBin--10,000
Total number of executed code statements---200,000
Total number of executed code statements for Batch Apex and future methods---1,000,000
Total heap size---6 MB
Total heap size for Batch Apex and future methods---12 MB
Total stack depth for any Apex invocation that recursively fires triggers due to insert, update, or delete statements--16
For loop list batch size--200
Total number of callouts (HTTP requests or Web services calls) in a request--10
Maximum timeout for all callouts (HTTP requests or Web services calls) in a request---120 seconds
Default timeout of callouts (HTTP requests or Web services calls) in a request---10 seconds
Total number of methods with the future annotation allowed per Apex invocation---10
Maximum size of callout request or response (HTTP request or Web services call)----3 MB
Total number of sendEmail methods allowed---10
Total number of describes allowed---100
Total number of classes that can be scheduled concurrently--25
Total number of test classes that can be queued per a 24–hour period
The greater of 500 or 10 multiplied by the number of test classes in the organization.
SOQL For Loops - Apex
Queries can be embedded in the special for syntax. This syntax can be used to loop through the sObjects returned by the query, one at a time, or in batches of 200 sObjects when using a list variable to hold the query results. Using a list variable to hold the query results in the SOQL for loop is a good way to query a large number of records since this helps avoid the heap limit, which is one of the governor execution limits.
Here is a SOQL for loop example. In this example, each iteration of the for loop operates on a single sObject returned by the query. This is inefficient if you perform database operations inside the for loop because they execute once for each sObject and you’re more likely to reach certain governor limits.
for (Merchandise__c tmp : [SELECT Id FROM Merchandise__c]) {
// Perform some actions on the single merchandise record.
}
A more efficient way is to use a list variable to hold the batch of records returned by each iteration of the for loop. This allows for bulk processing of database operations. The following example uses a list variable in the for loop.
for (Merchandise__c[] tmp : [SELECT Id FROM Merchandise__c]) {
// Perform some actions on the single merchandise record.
}
Hi...
Avoid Soql Query in For loop:http://wiki.developerforce.com/page/Best_Practice:_Avoid_SOQL_Queries_Inside_FOR_Loops
Understand Governors and Limits:
SOQL For Loops - Apex
Queries can be embedded in the special for syntax. This syntax can be used to loop through the sObjects returned by the query, one at a time, or in batches of 200 sObjects when using a list variable to hold the query results. Using a list variable to hold the query results in the SOQL for loop is a good way to query a large number of records since this helps avoid the heap limit, which is one of the governor execution limits.