Overview
ThePreparedStatement struct represents a CQL statement that has been prepared on the server. Prepared statements offer significant performance and safety benefits over unprepared statements.
Benefits
- Performance: Statement is parsed and planned only once on the server
- Type Safety: Values are validated against metadata during serialization
- Token-Aware Routing: Driver can compute partition tokens for optimal routing
- Result Metadata Caching: Reduces network overhead
Cloning
Cloning aPreparedStatement is cheap - it only copies Arc pointers and small fields. Always prefer cloning over re-preparing.
Source: scylla/src/statement/prepared.rs:202
Type Definition
Creation
Prepared statements are created viaSession::prepare:
Metadata Access
get_id
Returns the prepared statement ID.
Unique identifier assigned by the server
get_statement
Returns the original CQL statement text.
get_variable_col_specs
Returns column specifications for bind variables.
Metadata about bind markers (name, type, table)
get_variable_pk_indexes
Returns partition key indexes in the bind variables.
Positions and sequence of partition key columns
get_current_result_set_col_specs
Returns column specifications for the result set.
Snapshot of current result metadata (may be updated by server)
get_prepare_tracing_ids
Returns tracing IDs from statement preparation.
Table Information
get_table_spec
Returns the keyspace and table this statement operates on.
get_keyspace_name
get_table_name
Token and Routing
is_token_aware
Returns true if the statement can be routed in a token-aware manner.
true if partition key information is availableis_confirmed_lwt
Returns true if this is a confirmed lightweight transaction.
true if LWT optimization can be applied (ScyllaDB-specific)calculate_token
Computes the partition token for given bound values.
Bound values containing partition key columns
Computed token, or
None if statement is not token-awarecompute_partition_key
Computes the serialized partition key.
Values to serialize into partition key
Serialized partition key according to CQL protocol rules
get_partitioner_name
Returns the partitioner name used by the cluster.
Configuration Methods
Paging
set_page_size
Number of rows per page (must be positive)
page_size is non-positive.
get_page_size
Consistency
set_consistency
unset_consistency
get_consistency
Serial Consistency
set_serial_consistency
unset_serial_consistency
get_serial_consistency
Idempotence
set_is_idempotent
get_is_idempotent
Tracing
set_tracing
get_tracing
Result Metadata Caching
set_use_cached_result_metadata
Controls whether to use cached result metadata.
true to skip receiving metadata with each responseget_use_cached_result_metadata
Timestamp
set_timestamp
get_timestamp
Timeout
set_request_timeout
get_request_timeout
Policies
set_retry_policy
get_retry_policy
set_load_balancing_policy
get_load_balancing_policy
Execution Profile
set_execution_profile_handle
get_execution_profile_handle
History Listener
set_history_listener
remove_history_listener
Example Usage
Schema Changes and Repreparation
The driver automatically handles statement repreparation when the server invalidates cached statements due to schema changes.
When to Re-Prepare
You should drop and re-prepare a statement when:- Bind marker UDT changes: Adding fields to a UDT that is a bind marker
- Column type changes: Dropping and re-adding a bind marker column with different type (ScyllaDB-specific)
See Also
- Statement - Unprepared statements
- Batch - Batch multiple statements
- SerializeRow - Binding values to prepared statements
- Token-Aware Routing
