summaryrefslogtreecommitdiff
path: root/doc/src
diff options
context:
space:
mode:
authorFederico Di Gregorio <fog@initd.org>2010-02-22 19:38:47 +0100
committerFederico Di Gregorio <fog@initd.org>2010-02-22 19:38:47 +0100
commitb99f2d5f8e619c872a8d1b66741773fe1cda4b12 (patch)
treeb2f460f63ec48ce59b358564a0b5fc3048578e88 /doc/src
parent6733c64412509fbee22f044460b75123d3b49c26 (diff)
downloadpsycopg2-b99f2d5f8e619c872a8d1b66741773fe1cda4b12.tar.gz
Added a couple more questions to the FAQ
Diffstat (limited to 'doc/src')
-rw-r--r--doc/src/faq.rst24
1 files changed, 23 insertions, 1 deletions
diff --git a/doc/src/faq.rst b/doc/src/faq.rst
index 53f5b27..00501d7 100644
--- a/doc/src/faq.rst
+++ b/doc/src/faq.rst
@@ -78,4 +78,26 @@ I can't compile :mod:`!psycopg2`: the compiler says *error: libpq-fe.h: No such
You need to install the development version of the libpq: the package is
usually called ``libpq-dev``.
-
+When should I save and re-use a cursor as opposed to creating a new one as needed?
+ Cursors are lightweight objects and creating lots of them should not pose
+ any kind of problem. But note that cursors used to fetch result sets will
+ cache the data and use memory in proportion to the result set size. Our
+ suggestion is to almost always create a new cursor and dispose old ones as
+ soon as the data is not required anymore (call :meth:`~cursor.close` on
+ them.) The only exception are tight loops where one usually use the same
+ cursor for a whole bunch of INSERTs or UPDATEs.
+
+When should I save and re-use a connection as opposed to creating a new one as needed?
+ Creating a connection can be slow (think of SSL over TCP) so the best
+ practice is to create a single connection and keep it open as long as
+ required. It is also good practice to rollback or commit frequently (even
+ after a single SELECT statement) to make sure the backend is never left
+ "idle in transaction".
+
+What are the advantages or disadvantages of using named cursors?
+ The only disadvantages is that they use up resources on the server and
+ that there is a little overhead because a at least two queries (one to
+ create the cursor and one to fetch the initial result set) are issued to
+ the backend. The advantage is that data is fetched one chunk at a time:
+ using small :meth:`~cursor.fetchmany` values it is possible to use very
+ little memory on the client and to skip or discard parts of the result set.