Tell me more ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I've recently implemented a batch apex class that initially did not implement Database.Stateful. After using it for a while as-is I decided to implemente better error handling via a simple Map to store any error messages for records being processed and to email them to me in the finish(Database.BatchableContext) method.

All well and good, now on to the meat of my question: once this was in place I noticed a serious performance hit on my job's total runtime. (each execute seems to be roughly the same though). Why is this, and are there any other side effects to implementing or not implementing Database.Stateful?

share|improve this question
How big is the map storing the error messages getting? Being a stateful member variable it must be persisted somehow between batch transactions. If it got particularly large the serialization cost could become significant. – Daniel Ballinger Aug 21 '12 at 3:43
2  
That could certainly play a role but I've noticed a performance hit even when keeping something small like an integer or boolean in state. Would love it if someone more knowledgeable could chime in. – Greg Grinberg Aug 21 '12 at 4:46
1  
If you will permit me one more unknowledgeable conjecture. If the batches are stateful they must be orchestrated so that the member variables can be passed from the completion of one execution to the start of the next. Without the stateful constraint two executions could, in theory, run simultaneously. I have no proof or references to support this idea and would be interested in getting a definitive answer as well. – Daniel Ballinger Aug 21 '12 at 19:36

1 Answer

up vote 5 down vote accepted

Daniel Ballinger: No, batches do not ever run simultaneously. You are correct, however, that serialization is the culprit here.

grigriforce: what's your batch size? If you're doing a million records, and your batch size is 1, then you will serialize/deserialize your state 1M times. Even with a small serialized object, that's gonna hurt.

share|improve this answer
Thank you for that, I could swear I once had a senior member of SFDC R&D mention there are 7 worker threads per org, which doesn't make sense if batches are never run in parallel. I may be mis-remembering though. – ca_peterson Aug 22 '12 at 20:48
perhaps getting batch apex and the bulk api mixed up? (the bulk api can do parallel batches) – superfell Aug 23 '12 at 5:05
@superfell could be. Or maybe there's some hidden soft limits or other magic for the future. Or may I misheard. Either way between your and Rich's response I'm pretty confident it's not what I thought it was. Thanks to you both for confirming. – ca_peterson Aug 25 '12 at 4:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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