Skip to content

Commit

Permalink
book: mention benefits of prepared statements
Browse files Browse the repository at this point in the history
  • Loading branch information
muzarski committed Apr 25, 2024
1 parent 5c97438 commit 880d7b0
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions docs/source/queries/prepared.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# Prepared query

Prepared queries provide much better performance than simple queries,
Prepared queries provide much better performance than unprepared queries,
but they need to be prepared before use.

There are other benefits that prepared queries have to offer:
- Increased type-safety - thanks to metadata provided by the server, the driver can verify bound values' types before serialization. This way, we can be always sure that the Rust type provided by the user is compatible (and if not, the error is returned) with the destined native type. The same applies for deserialization.
- Improved load-balancing - using the statement metadata, the driver can compute a set of destined replicas for current statement execution. These replicas will be preferred when choosing the node (and shard) to send the request to. For more insight on this, see [performance section](#performance).

```rust
# extern crate scylla;
# use scylla::Session;
Expand All @@ -15,7 +19,7 @@ let prepared: PreparedStatement = session
.prepare("INSERT INTO ks.tab (a) VALUES(?)")
.await?;

// Run the prepared query with some values, just like a simple query
// Run the prepared query with some values, just like an unprepared query
let to_insert: i32 = 12345;
session.execute(&prepared, (to_insert,)).await?;
# Ok(())
Expand All @@ -39,7 +43,7 @@ If at least one succeeds returns success.

### `Session::execute`
`Session::execute` takes a prepared query and bound values and runs the query.
Passing values and the result is the same as in [simple query](simple.md).
Passing values and the result is the same as in [unprepared query](simple.md).

### Query options

Expand All @@ -63,7 +67,7 @@ let mut prepared: PreparedStatement = session
// This is the consistency with which this query will be executed
prepared.set_consistency(Consistency::One);

// Run the prepared query with some values, just like a simple query
// Run the prepared query with some values, just like an unprepared query
let to_insert: i32 = 12345;
session.execute(&prepared, (to_insert,)).await?;
# Ok(())
Expand All @@ -80,7 +84,7 @@ for more options.
### Performance

Prepared queries have good performance, much better than simple queries.
Prepared queries have good performance, much better than unprepared queries.
By default they use shard/token aware load balancing.

> **Always** pass partition key values as bound values.
Expand Down

0 comments on commit 880d7b0

Please sign in to comment.