Sugarbean bulk save

I’m currently working through optimizing some of my team’s legacy code and through profiling I’ve found that a lot of our issues stem from bean->save() being invoked inside loops.

I would like to have the pending beans all saved in bulk rather than to individually save each one. Am I missing a method which can do this?

If that isn’t possible then what are the drawbacks of manually inserting the rows for the module into the db? Is this a viable workflow or is there too much logic being managed in the save() method to be worth doing myself?

Help much appreciated.

What do you mean, in bulk? What is the actual operation you’re doing on the Beans? Is it from the list view?

It is certainly a possible path, but the implications have to be studied case by case. Some modules are more tied into others. Some “saves” affect more than one module, or a module and a relationship. You need to study the actual operation(s) you’re most interested on optimizing and create a bulk process, possibly with direct SQL.

Hi,
If you skip beans, workflow related to module records actions and logic hooks will got skipped. Additionally any formatting like for text area field will not be implemented as well.
So if you are saving some fields, let say 5,6 , then it is good to got with DB query. But if your module has a lot of fields and different field types, then you can use Bean. But bean consumes your server RAM a lot. Using it in a LOOP is not a good thing.

Thanks

Thanks for the replies!

@pgr when we convert a quote to an invoice we are generating a number of subsidies which in turn create new invoices and a number of other items. We need to save a number of new beans when this happens which seems to be responsible for these slowdowns.

@suitecrm_developer @pgr thanks for the advice, I’m beginning to think that instead of optimizing the current workflow that it needs to be completely redesigned as I am unsure if there is a performant solution with the current workflow. I’m now convinced that directly inserting into the db is going to be a world of pain and I should try to avoid it at all costs.

Appreciate the advice! Have a good one.

1 Like

How many? If it is a small number like 5 or 6, and you don’t have dozens of users doing it simultaneously, you should be able to do it without much hassle. If not, then maybe there’s something slowing down your system, like lack of PHP memory, slow logic hooks, or infinite recursion of logic hooks or workflows, or something else.

I didn’t say that writing directly to the DB would be “world of pain and I should try to avoid it at all costs”… generally, it should be avoided, but not at all costs. Sometimes it’s a good option. And all it requires is a basic understanding of how things are stored in the DB, which is a good thing to have in any case.