Is there a faster way todo this?

await db.transaction(async (tx) => {
for (let i = 0; i < data.length; i++) {
tx.update(cities)
.set({ name: data[i].name })
.where(eq(cities.id, data[i].id));
}
});
await db.transaction(async (tx) => {
for (let i = 0; i < data.length; i++) {
tx.update(cities)
.set({ name: data[i].name })
.where(eq(cities.id, data[i].id));
}
});
This takes about 250ms for me, I find this very slow...
1 Reply
Angelelz
Angelelz16mo ago
If id is the primary key or an unique index you could do:
db
.insert(cities)
.values(data)
.onDuplicateKeyUpdate(
{
set: { name: sql`VALUES(name)` }
}
)
db
.insert(cities)
.values(data)
.onDuplicateKeyUpdate(
{
set: { name: sql`VALUES(name)` }
}
)
It feels like that should be faster, but you'll have to do your own tests Also, you should check the length of the array, I believe arrays over certain length will throw errors
Want results from more Discord servers?
Add your server