diff options
author | Daniele Varrazzo <daniele.varrazzo@gmail.com> | 2017-02-02 02:40:03 +0000 |
---|---|---|
committer | Daniele Varrazzo <daniele.varrazzo@gmail.com> | 2017-02-02 02:40:03 +0000 |
commit | 27652ed3b0afda8d175fa673ecd4e341b3a58c3c (patch) | |
tree | b1160c7decadd5dcf13cde7d65edb12c6c079be8 | |
parent | 9bac37baf76dbd0831230e4d01d5269b6e4a9438 (diff) | |
download | psycopg2-27652ed3b0afda8d175fa673ecd4e341b3a58c3c.tar.gz |
Added notes about using execute_batch with prepared statements
-rw-r--r-- | doc/src/extras.rst | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/doc/src/extras.rst b/doc/src/extras.rst index 66be590..5ef4223 100644 --- a/doc/src/extras.rst +++ b/doc/src/extras.rst @@ -993,6 +993,38 @@ parameters. By reducing the number of server roundtrips the performance can be .. versionadded:: 2.7 +.. note:: + + `!execute_batch()` can be also used in conjunction with PostgreSQL + prepared statements using |PREPARE|_, |EXECUTE|_, |DEALLOCATE|_. + Instead of executing:: + + execute_batch(cur, + "big and complex SQL with %s %s params", + params_list) + + it is possible to execute something like:: + + cur.execute("PREPARE stmt AS big and complex SQL with $1 $2 params") + execute_batch(cur, "EXECUTE stmt (%s, %s)", params_list) + cur.execute("DEALLOCATE stmt") + + which may bring further performance benefits: if the operation to perform + is complex, every single execution will be faster as the query plan is + already cached; furthermore the amount of data to send on the server will + be lesser (one |EXECUTE| per param set instead of the whole, likely + longer, statement). + + .. |PREPARE| replace:: :sql:`PREPARE` + .. _PREPARE: https://www.postgresql.org/docs/current/static/sql-prepare.html + + .. |EXECUTE| replace:: :sql:`EXECUTE` + .. _EXECUTE: https://www.postgresql.org/docs/current/static/sql-execute.html + + .. |DEALLOCATE| replace:: :sql:`DEALLOCATE` + .. _DEALLOCATE: https://www.postgresql.org/docs/current/static/sql-deallocate.html + + .. autofunction:: execute_values .. versionadded:: 2.7 |