New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to execute multiple UPDATES at once? #1517

Closed
fafhrd91 opened this Issue Jan 30, 2018 · 8 comments

Comments

Projects
None yet
4 participants
@fafhrd91

fafhrd91 commented Jan 30, 2018

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

could anyone point me to a api doc or example?

@sgrif

This comment has been minimized.

Member

sgrif commented Jan 30, 2018

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

@fafhrd91

This comment has been minimized.

fafhrd91 commented Jan 30, 2018

something like:

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

This comment has been minimized.

Member

sgrif commented Jan 30, 2018

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)?;

@sgrif sgrif closed this Jan 30, 2018

@fafhrd91

This comment has been minimized.

fafhrd91 commented Jan 30, 2018

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

@sgrif

This comment has been minimized.

Member

sgrif commented Jan 30, 2018

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).

@fafhrd91

This comment has been minimized.

fafhrd91 commented Jan 30, 2018

Thanks.

@thomasetter

This comment has been minimized.

thomasetter commented Sep 26, 2018

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

@mjanda

This comment has been minimized.

mjanda commented Sep 26, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment