Score:0

Save multiple entities in one transaction

br flag

We have a custom API endpoint that handles data for multiple users. Unfortunately, we are having a performance issue because every time we save a user entity, the save operation takes 3-4 seconds to complete. Obviously, this is less than ideal, as 10 users will take 30-40 seconds to complete the whole request...

Is there a way to save multiple entities at once (like you can load multiple at once) or is this just another poor Drupal performance issue that we have to live with?

cn flag
I'm pretty sure there's no method for that in core; does the user entity have a lot of fields in your site? Do other entities (different types like node) with a similar number of fields take the same sort of time to save? Are you measuring the full request at 3-4 seconds or have you profiled the user save op specifically at that length? If the former you should be able to deduct a second or two of that from the estimate for each entity as that'll be one-time request overhead
BlondeSwan avatar
br flag
depends on what you consider "a lot of fields", we have about 80 fields on our user object. In this particular endpoint though, we are updating only 5 of those fields: 2 multiple value references, and 3 multiple value text fields.
BlondeSwan avatar
br flag
This is just the user save. It takes about 3-4 seconds for `JSON::decode($request->getContent)` to run so that I can work with the data (which is horrendous), then < 1 second to do all my work, then another 3-4 seconds to save the user. So the time it takes to process users is ` 3 seconds + 3 seconds per user`.
cn flag
Yeah that's a lot of fields, but actually in theory that shouldn't matter (https://drupal.stackexchange.com/a/234475/2800). Best advice I can give it to profile the save op and find out where the time is being spent - it might not be database, which would hopefully give you other threads to follow to improve it, but either way you'd know
BlondeSwan avatar
br flag
The save operation itself takes 3-4 seconds.
cn flag
I meant [profile it a bit more scientifically](https://xdebug.org/docs/profiler), that'll show you where the time is really being spent. If you're lucky your hosting provider might have something like New Relic available
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.