[SalesForce] What are the implications of implementing Database.Stateful

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?

Best Answer

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.

Related Topic