Was this page helpful?
Caution
You're viewing documentation for a deprecated version of Scylla Rust Driver. Switch to the latest stable version.
A batch statement allows to execute many data-modifying statements at once.
These statements can be simple or prepared.
Only INSERT
, UPDATE
and DELETE
statements are allowed.
use scylla::batch::Batch;
use scylla::query::Query;
use scylla::prepared_statement::PreparedStatement;
// Create a batch statement
let mut batch: Batch = Default::default();
// Add a simple statement to the batch using its text
batch.append_statement("INSERT INTO ks.tab(a, b) VALUES(1, 2)");
// Add a simple statement created manually to the batch
let simple: Query = Query::new("INSERT INTO ks.tab (a, b) VALUES(3, 4)");
batch.append_statement(simple);
// Add a prepared statement to the batch
let prepared: PreparedStatement = session
.prepare("INSERT INTO ks.tab (a, b) VALUES(?, 6)")
.await?;
batch.append_statement(prepared);
// Specify bound values to use with each statement
let batch_values = ((),
(),
(5_i32,));
// Run the batch
session.batch(&batch, batch_values).await?;
Warning
Using simple statements with bind markers in batches is strongly discouraged. For each simple statement with a non-empty list of values in the batch, the driver will send a prepare request, and it will be done sequentially. Results of preparation are not cached betweenSession::batch
calls. Consider preparing the statements before putting them into the batch.
Instead of preparing each statement individually, it’s possible to prepare a whole batch at once:
use scylla::batch::Batch;
// Create a batch statement with unprepared statements
let mut batch: Batch = Default::default();
batch.append_statement("INSERT INTO ks.simple_unprepared1 VALUES(?, ?)");
batch.append_statement("INSERT INTO ks.simple_unprepared2 VALUES(?, ?)");
// Prepare all statements in the batch at once
let prepared_batch: Batch = session.prepare_batch(&batch).await?;
// Specify bound values to use with each statement
let batch_values = ((1_i32, 2_i32),
(3_i32, 4_i32));
// Run the prepared batch
session.batch(&prepared_batch, batch_values).await?;
You can set various options by operating on the Batch
object.
For example to change consistency:
use scylla::batch::Batch;
use scylla::statement::Consistency;
// Create a batch
let mut batch: Batch = Default::default();
batch.append_statement("INSERT INTO ks.tab(a) VALUES(16)");
// Set batch consistency to One
batch.set_consistency(Consistency::One);
// Run the batch
session.batch(&batch, ((), )).await?;
See Batch API documentation for more options
Batch takes a tuple of values specified just like in simple or prepared queries.
Length of batch values must be equal to the number of statements in a batch.
Each statement must have its values specified, even if they are empty.
Values passed to Session::batch
must implement the trait BatchValues
.
By default this includes tuples ()
and slices &[]
of tuples and slices which implement ValueList
.
Example:
use scylla::batch::Batch;
let mut batch: Batch = Default::default();
// A statement with two bound values
batch.append_statement("INSERT INTO ks.tab(a, b) VALUES(?, ?)");
// A statement with one bound value
batch.append_statement("INSERT INTO ks.tab(a, b) VALUES(3, ?)");
// A statement with no bound values
batch.append_statement("INSERT INTO ks.tab(a, b) VALUES(5, 6)");
// Batch values is a tuple of 3 tuples containing values for each statement
let batch_values = ((1_i32, 2_i32), // Tuple with two values for the first statement
(4_i32,), // Tuple with one value for the second statement
()); // Empty tuple/unit for the third statement
// Run the batch
// Note that the driver will prepare the first two statements, due to them
// not being prepared and having a non-empty list of values.
session.batch(&batch, batch_values).await?;
For more information about sending values in a statement see Query values
Batch statements do not use token/shard aware load balancing, batches are sent to a random node.
Use prepared queries for best performance
Was this page helpful?