Queries on batch execute

batch

I still don't know if queries in execute batch are made for each record or batch execution. For instance:

global void execute(Database.BatchableContext BC, List<xR1_order__c> scope){
 
    SELECT....
 
    SELECT...
 
    SELECT...
 
    for(xR1_order__c order:scope){
    ...
    }

}

Let's assume that batch limit is 200 records. Will those queries be made 200 times even if they are not in the (for…scope) or just once for the 200 records?

Thanks so much

Best Answer

Apex is Apex. It does exactly what you tell it to do, as do most programming languages. If you write your code to handle a bulk scope, each query should execute only once per execute method. Note that if you have a scope of 200, and you are processing 500 records, there will be three execute calls, and thus each query will be called three times--but not in the same transaction. Governor limits reset at the start of each execute call, so you don't have to worry about running into limits, as long as your code is optimized and each execute call does not individually exceed any limits.