Skip to content
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

Use postgres Transactions to optimize bulk insertions #1

Closed
DieHard073055 opened this issue Sep 20, 2023 · 0 comments
Closed

Use postgres Transactions to optimize bulk insertions #1

DieHard073055 opened this issue Sep 20, 2023 · 0 comments

Comments

@DieHard073055
Copy link
Owner

By bundling many insertions into a single transaction, you reduce the overhead of transactional logging. Each individual transaction (commit) in PostgreSQL involves disk I/O operations for logging, so reducing the number of commits can speed up bulk operations.

Here is an example

// initiate the transaction
let mut tx = pool.begin().await?;

// execute a query into the transaction
sqlx::query!("INSERT INTO your_table (column1, column2) VALUES ($1, $2)", value1, value2)
    .execute(&mut tx)
    .await?;

// execute more queries if you have any

// Then commit the transaction when you are done.
tx.commit().await?;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant