For a new batch process I have a choice between immediately selecting relevant records in the start function (but using several [field]__r comparisons as they are junction objects being queried) or creating a new field on the records marking them for batch processing in an earlier stage and then perform an additional query in execute to 'manually' filter out the correct objects.

I remember reading that relationship fields and such all increase processing time, but is this impact big enough to warrant creating a new field and adding an extra query in execute ?


I would advise against selecting too broad in start() and then narrowing the results further in execute(). This will only maximize the risk of hitting some platform limits and affect the performance and execution time.

But it is advisable to limit the numer of fields, which you are selecting at start() and querying them at the execute() method. We are using this pattern widely and it solved a lot of query timeouts at the start() methods of our batches. Short example:

public Database.QueryLocator start(Database.BatchableContext context){
    return Database.getQueryLocator(
        [SELECT Id
         FROM Contact
         WHERE LeadSource = 'Web'
         AND Account.Type = 'Prospect'

public void execute(Database.BatchableContext context, List<Contact> rawContacts){
    List<Contact> contacts = [
        SELECT Id, LastName, FirstName, Account.Name, Account.Type,
            (SELECT Id, CaseNumber FROM Cases)
        FROM Contact
        WHERE Id IN :rawContacts

    // further processing
|improve this answer|||||

Yes, Right! Alexander has given a prompt answer upon which i will add some more points.

There are cases where you need to have this in Start(), For example. If the Query hits

Too Many Query Rows : 50001 error

, Then you might need to move the query to start as the Limit is not applicable there.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.