Total Pageviews

Saturday, February 1, 2014

Batch Apex – What you should be careful about?

I have been writing or reviewing codes in Salesforce for over six years now. Whenever one comes across a tricky situation which involves processing huge volumes of data “Batch Apex” is the way to go, but every now and then you may be alarmed by someone telling you – that hey I hit the 10k limit while executing a Batch Job!

You would wonder- how is that possible? Soon you would realize that its the result of one the common slips that one does while designing a “Batch Job”. Most commonly we fetch records in access of 10,000 within the Execute() method. Treat all code written within execute() method as if you are writing for normal Apex.
Batch Apex surely has more flexible Governor Limits for example:-
·         200  SOQL per cycle (Please check the latest limit a s per latest release)
·         Records retrieved by SOQL query 50,000,000 as against 50k in the normal apex.
·         Heap Size is much larger than that compared to normal apex.
Why Batch Job is capable of handling huge data sets?
 One has to understand what makes Batch Apex successful in processing huge data sets:-
·         The large data set is processed in batches – which implies that the parameter “query” in  Start Method () that has to be the query which will return the bulk of data. Please refer to following code snippet for clarity:-
global Database.QueryLocator start(Database.BatchableContext BC){
 return Database.getQueryLocator(query);
Common Pitfalls
è You might say well what’s new in that we all know that. Well true but just reiterating -that the dataset which is the largest - it has to be fetched with the SOQL in “Query” and then processed in batches
è Always remember within the Execute() method never execute an Insert or update which will process more that 10k records in a cycle.
·         Also the enhanced governor limits enables the Batch Apex to process huge datasets, but one needs to cautious that though relaxed the governor still exist w.r.t. Batch Apex too. The following may be important for considering while finalizing your design and organization wide setup:-
o   Batch Apex Governor Limits
o   These are the governor Limits you need to keep in mind when dealing with Batch Apex
o   Up to five queued or active batch jobs are allowed for Apex.
o   A user can have up to 50 query cursors open at a time. For example, if 50 cursors are open and a client application still logged in as the same user attempts to open a new one, the oldest of the 50 cursors is released. Note that this limit is different for the batch Apex start method, which can have up to five query cursors open at a time per user. The other batch Apex methods have the higher limit of 50 cursors. Cursor limits for different features are tracked separately. For example, you can have 50 Apex query cursors, 50 batch cursors, and 50 Visualforce cursors open at the same time.
o   A maximum of 50 million records can be returned in the Database.QueryLocator object. If more than 50 million records are returned, the batch job is immediately terminated and marked as Failed. So if you have a requirement to process in excess of that value factor in the necessary checks and balances in your design.
o   If the start method returns a QueryLocator, the optional scope parameter of Database.executeBatch can have a maximum value of 2,000. If you set to a higher value it will anyways break it into smaller chunks, which is a blessing in a sense.
o   If no size is specified with the optional scope parameter of Database.executeBatch, Salesforce chunks the records returned by the start method into batches of 200, and then passes each batch to the execute method. Apex governor limits are reset for each execution of execute.
o   The start, execute, and finish methods can implement up to 10 callouts each.
o   Batch executions are limited to 10 callouts per method execution.
o   The maximum number of batch executions is 250,000 per 24 hours.
o   Only one batch Apex job's start method can run at a time in an organization. Batch jobs that haven’t started yet remain in the queue until they're started. Note that this limit doesn't cause any batch job to fail and execute methods of batch Apex jobs still run in parallel if more than one job is running.

No comments:

Post a Comment