Re: Using cursor
Jeroen T. Vermeulen <
jtv@...>
2007-06-03 07:33:41 GMT
On Thu, May 31, 2007 14:17, Trigve Siver wrote:
> I mean if there is some diffrence using pqxx Transaction.exec() and
> pqxx::absolute_cursor. AFAIK cursor read results "on demand" while
> Transaction.exec()
> return whole result for executed query. Is it true? Can be there some
> performance gain when using cursors?
Ah, so it's not really about absolute_cursor but about cursors in general.
You understood correctly. While a cursor only returns chunks of the work,
and only when you ask for them, transaction_base::exec() simply reads the
whole result set at once.
When talking purely about the overall speed it takes to retrieve all your
results, and in a normal program, a cursor will always be slower than
exec(). Don't complicate your code with cursors if you don't need to!
So why do we have cursors at all? They make sense in many cases:
* If your result set can be huge, and you only look at one row at a time,
there's no reason to keep the entire result set in memory. With a cursor
you can download and process just one chunk at a time.
* If you want to give quick first results, and it takes too long to get
the whole result set, use a cursor to retrieve and process a bit of your
data relatively soon.
* If you want to give regular progress reports, e.g. a progress bar, on
large result sets, break them up into chunks with a cursor.
(Continue reading)