Diesel: Is it possible to execute multiple UPDATES at once?

Created on 30 Jan 2018  路  9Comments  路  Source: diesel-rs/diesel

I couldn't find any information on possibility to execute multiple updates.

could anyone point me to a api doc or example?

Most helpful comment

something like:

UPDATE posts SET field='1' WHERE ..;
UPDATE posts SET field='2' WHERE ..;
UPDATE posts SET field='2' WHERE ..;

All 9 comments

Can you expand on what you mean by "execute multiple updates"? Can you provide the SQL query you'd like to construct?

something like:

UPDATE posts SET field='1' WHERE ..;
UPDATE posts SET field='2' WHERE ..;
UPDATE posts SET field='2' WHERE ..;

You just need to call execute on multiple update statements. e.g.

update(posts).set(field.eq("1")).filter(...).execute(&conn)?;
update(posts).set(field.eq("2")).filter(...).execute(&conn)?;

would it execute each update separately?

some background, I use diesel for TechEmpower framework benchmarks and it performs very well.
but result for "db update" benchmark is very weak. I see other frameworks do batch updates

https://github.com/TechEmpower/FrameworkBenchmarks/tree/master/frameworks/Rust/actix

I haven't seen much real world usage that needs to execute multiple unrelated update statements in a single round trip. If you want to optimize for that benchmark, you can pass a SQL string to connection.batch_execute (this is how migrations are run).

Thanks.

Another approach which also works with prepared statements is to chain the updates with WITH to create a single statement containing several updates:
https://dba.stackexchange.com/questions/171123/use-same-postgres-with-in-multiple-queries

It seems like it's using update ... from syntax and building that update sql manually.

https://github.com/TechEmpower/FrameworkBenchmarks/blob/master/frameworks/Rust/actix/src/db_pg.rs#L180

Nice, didn't even know postgresql can do that.

First off thanks for the library!

I'm hitting this issue trying to batch upsert. I want to sync against a remote source of truth, but have local records related to that.

I tried playing around with making a Vec of Changesets, but hit a bunch of dead ends before googling and finding this.

Would a pull request be welcomed?

Was this page helpful?
0 / 5 - 0 ratings