Update performance issue when performing many single update() operations

I'm having big performance issues trying to update many rows inside a transaction here's the code: I tried to parallelise the updates with a Promise.all but no improvements. I have about 50 elements in data and it takes an astonishing 4 seconds to do such a simple update. Note: I cannot use the updateMany because I need to update each rows with specific data, I used Total: 1234 here to simplify the example
await prisma.$transaction(async (t) => {
const promises = data.map((d) => {
return prismaClient.myTable.update({
where: { ID: d.ID },
data: {
Total: 1234,
},
});
});

await Promise.all(promises);
});
await prisma.$transaction(async (t) => {
const promises = data.map((d) => {
return prismaClient.myTable.update({
where: { ID: d.ID },
data: {
Total: 1234,
},
});
});

await Promise.all(promises);
});
5 Replies
Prisma AI Help
Prisma AI Help6mo ago
You chose to debug with a human. They'll tinker with your query soon. If you get curious meanwhile, hop into #ask-ai for a quick spin!
danbord#3379
danbord#3379OP6mo ago
I realized that I could use updateMany to nearly double the speed. Probably because there is no return of the entity after an updateMany Any other improvements that can be done?
unrok4009
unrok40096mo ago
db calls in transactions run on a single connection so Promise.all() has no effect
Nurul
Nurul6mo ago
Did you see any improvements after using updateMany?
danbord#3379
danbord#3379OP6mo ago
Absolutely, speed is twice faster now.

Did you find this page helpful?