6

My previous question about slow Batches probably was to focused on a specific problem, so I try it again much broader.

What are general rules of thumb to make a slow Batch faster?

Although I added 2 of my own findings as answers, I want to invite others to contribute answers as well.

Robert Sösemann
  • 37,622
  • 26
  • 165
  • 495

2 Answers2

4

Get rid of implements Database.Stateful

Serializing and deserializing state (values of member variables in you Batch class) needs time and is performed for each single job (call of execute())

Robert Sösemann
  • 37,622
  • 26
  • 165
  • 495
4

Use [Object].field = value instead of SObject.get() / SObject.put() .

This was mentioned by Salesforce.com support when we asked them why Batch code was so slow. We read and set arbitrary SObject fields we do not know in advance. So we had to resort to the generic functions. This caused a serious slow-down.

Robert Sösemann
  • 37,622
  • 26
  • 165
  • 495
  • One approach to combine the generic flexibility and the speed of specific field assignments is to generate the assignment code during runtime. This is generally possible with something like the Metadata API (https://github.com/financialforcedev/apex-mdapi/) – Robert Sösemann Dec 09 '14 at 08:31