a big loop of `.upsert` calls is a bottleneck in ...
# orm-help
t
a big loop of
.upsert
calls is a bottleneck in my app, so I'm trying to switch to either
createMany
with ignoring duplicates, or
queryRaw
. I'm hitting the issues that (1) there's no
upsertMany
. (2)
createMany
doesn't return the duplicated rows and
updateMany
doesn't let you specify multiple
where
conditions, and (3)
queryRaw
doesn't appear to support arrays-of-arrays as arguments. This is a bit of a bummer. I'm trying to just have some raw SQL in this part of my app because the ORM-unoptimized version is not sufficient, but the queryRaw bit doesn't seem to work either for this usecase.
a
I wrestled with this recently too
Wound up writing out a queryRaw that looped over each of the records with an INSERT ON CONFLICT DO UPDATE
ran into an issue parameterizing floats, but didn't have to deal with arrays-as-fields
t
i was able to make a little progress, by dropping down to lower levels of the stack
ingredient was doing more casting,
::jsonb
not sure about arrays-of-arrays yet
a
ahh, tricky. This is a really tough use case -- Prisma does so many things well, but it hurts to have to drop to raw SQL strings for batch updates, when you may not have to with some other ORMs (that are deficient in other ways!)
t
okay, finally got it in!
depended on
UNNEST
, lots of explicit casting with
::
, and debugging the query outside of prisma
it's dirty work, but it's a 10x+ speedup for a bottlenecked method 🙂
what this looks like on the root level, for future adventurers… https://gist.github.com/tmcw/0e3ab507d68c9dfcadb9bcad81ea6ac6
a
Thanks for sharing!
the
$3::int[]
syntax is new to me. looks useful.
t
zip
here is the lodash zip method, which changes an array of [a, b, c] into arrays of [a, a, a], etc, then you unnest that into a list of rows you select from, cast, etc