Using Batch Apex on millions of records

Part of our app behaviour is going over millions on records and updating their data.

From what I read, we can do it using Batch Apex although I am not certain I fully understand how it works.

Here is a code snippet of our Batchable class:

       global class EnrichContacts implements Database.Batchable<sObject>
            global final String Query;

            global EnrichContacts(String q)

            global Database.QueryLocator start(Database.BatchableContext BC)
               return Database.getQueryLocator(query);

            global void execute(Database.BatchableContext BC,List<sObject> scope)
                  List <Contact> modifiedContacts = new list<Contact>();
                  for(Sobject s : scope)
                      Contact a = (Contact)s;
                      // Add some information to the contact
                  update modifiedContacts;

            global void finish(Database.BatchableContext BC)

       //Calling the Batch
       id bId = database.executeBatch(new EnrichContacts(‘select Id from Contact’))

My Concerns:

  • Will this code works on millions of contacts?
  • Does runtime calls the “execute” method each time with 50k contacts?
  • Does the execute method has a time-out after X seconds?
  • Can we control the amount of records being delivered to the “execute” method? (for example if we wish to deliver each time 500 contacts)

Appreciate any ideas…


Answers to your concerns :

1)Yes batch apex are asynchronous process and can support upto 50 million records and hence for 1 million this code will run

2)You will specify batch size while you invoke batch .Say i specify batch size as 400.Then 50000/400 number of batches will run.In each batch context 400 records will picked .

3)No there are no time outs as such till Summer 13 .There are heap size limits and script statements that you would need to be careful .From next release there is no script statements limit but there is some time out limit for synchronous calls and 60000 milliseconds of CPU time for async transactions.

4)Yes we can control this through the scheduler script by specifying the batch size

String query = 'select Id from Contact'
    EnrichContacts batchpr = new EnrichContacts (query);
    Id batchprocessId = Database.executeBatch(batchpr,500);

The above script that you have i have split and 500 is my batch size .

Source : Link , Question Author : Uri Lukach , Answer Author : Mohith Shrivastava

Leave a Comment