Skip to content

Commit 65e898a

Browse files
authored
Merge pull request scylladb#35 from riptano/python-1167
PYTHON-1167: emphasize prepared statements in getting started
2 parents c4066a5 + ef0831b commit 65e898a

File tree

1 file changed

+36
-33
lines changed

1 file changed

+36
-33
lines changed

docs/getting_started.rst

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ by executing a ``USE <keyspace>`` query:
110110
# or you can do this instead
111111
session.execute('USE users')
112112
113-
114113
Executing Queries
115114
-----------------
116115
Now that we have a :class:`.Session` we can begin to execute queries. The simplest
@@ -153,13 +152,45 @@ examples are equivalent:
153152
If you prefer another result format, such as a ``dict`` per row, you
154153
can change the :attr:`~.Session.row_factory` attribute.
155154

156-
For queries that will be run repeatedly, you should use
157-
`Prepared statements <#prepared-statements>`_.
155+
As mentioned in our `Drivers Best Practices Guide <https://docs.datastax.com/en/devapp/doc/devapp/driversBestPractices.html#driversBestPractices__usePreparedStatements>`_,
156+
it is highly recommended to use `Prepared statements <#prepared-statement>`_ for your
157+
frequently run queries.
158+
159+
.. _prepared-statement:
160+
161+
Prepared Statements
162+
-------------------
163+
Prepared statements are queries that are parsed by Cassandra and then saved
164+
for later use. When the driver uses a prepared statement, it only needs to
165+
send the values of parameters to bind. This lowers network traffic
166+
and CPU utilization within Cassandra because Cassandra does not have to
167+
re-parse the query each time.
168+
169+
To prepare a query, use :meth:`.Session.prepare()`:
170+
171+
.. code-block:: python
172+
173+
user_lookup_stmt = session.prepare("SELECT * FROM users WHERE user_id=?")
174+
175+
users = []
176+
for user_id in user_ids_to_query:
177+
user = session.execute(user_lookup_stmt, [user_id])
178+
users.append(user)
179+
180+
:meth:`~.Session.prepare()` returns a :class:`~.PreparedStatement` instance
181+
which can be used in place of :class:`~.SimpleStatement` instances or literal
182+
string queries. It is automatically prepared against all nodes, and the driver
183+
handles re-preparing against new nodes and restarted nodes when necessary.
184+
185+
Note that the placeholders for prepared statements are ``?`` characters. This
186+
is different than for simple, non-prepared statements (although future versions
187+
of the driver may use the same placeholders for both).
158188

159189
Passing Parameters to CQL Queries
160190
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
161-
When executing non-prepared statements, the driver supports two forms of
162-
parameter place-holders: positional and named.
191+
Althought it is not recommended, you can also pass parameters to non-prepared
192+
statements. The driver supports two forms of parameter place-holders: positional
193+
and named.
163194

164195
Positional parameters are used with a ``%s`` placeholder. For example,
165196
when you execute:
@@ -376,34 +407,6 @@ in a :class:`~.SimpleStatement`:
376407
consistency_level=ConsistencyLevel.QUORUM)
377408
session.execute(query, ('John', 42))
378409
379-
Prepared Statements
380-
-------------------
381-
Prepared statements are queries that are parsed by Cassandra and then saved
382-
for later use. When the driver uses a prepared statement, it only needs to
383-
send the values of parameters to bind. This lowers network traffic
384-
and CPU utilization within Cassandra because Cassandra does not have to
385-
re-parse the query each time.
386-
387-
To prepare a query, use :meth:`.Session.prepare()`:
388-
389-
.. code-block:: python
390-
391-
user_lookup_stmt = session.prepare("SELECT * FROM users WHERE user_id=?")
392-
393-
users = []
394-
for user_id in user_ids_to_query:
395-
user = session.execute(user_lookup_stmt, [user_id])
396-
users.append(user)
397-
398-
:meth:`~.Session.prepare()` returns a :class:`~.PreparedStatement` instance
399-
which can be used in place of :class:`~.SimpleStatement` instances or literal
400-
string queries. It is automatically prepared against all nodes, and the driver
401-
handles re-preparing against new nodes and restarted nodes when necessary.
402-
403-
Note that the placeholders for prepared statements are ``?`` characters. This
404-
is different than for simple, non-prepared statements (although future versions
405-
of the driver may use the same placeholders for both).
406-
407410
Setting a Consistency Level with Prepared Statements
408411
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
409412
To specify a consistency level for prepared statements, you have two options.

0 commit comments

Comments
 (0)