pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Oliver Jowett <oliver@opencloud.com>
To: Maik Wiege <mswiege@gmx.de>
Cc: pgsql-jdbc@postgresql.org
Subject: Re: get row count from a "cursor resultset"
Date: Thu, 07 Jul 2005 10:27:26 +1200
Message-ID: <42CC5ACE.5070200@opencloud.com> (raw)
In-Reply-To: <42CC2EB6.8020505@gmx.de>
References: <42CC2EB6.8020505@gmx.de>

Maik Wiege wrote:
> Hello!
> I'm querying my database and sometimes get a very big resultset back, so
> I needed to use a resultset with a cursor to avoid a OutOfMemoryError.
> 
> Now, because I have to use a TYPE_FORWARD_ONLY resultset for that, I can
> not use the result.last() function anymore to retrieve the amount of
> rows the query returns.
> 
> The query is a little bit complicated and that for can take some time,
> so I don't want to do the "SELECT COUNT(*) FROM (SELECT...)" afterward,
> what would result in doing the query twice and there for would double
> the time needed.

Have you tried using COUNT directly in your original query (not as a
separate query)? I wouldn't expect that to affect total execution time
much if at all; the main difference will be that the full query has to
complete before you get any results, while without the COUNT you might
start getting results immediately.

There's not really any other option that the current driver code will do
for you. You could DECLARE CURSOR / MOVE / FETCH yourself perhaps; I'd
expect this to have similar startup characteristics to the COUNT(*) case
though. Even if the driver supported cursor-based random-access
resultsets, it'd be using MOVE / FETCH under the covers in the same way..

> I didn't find a solution for this, but there must be, because the pgAdmin III - tool does this. After starting the query in the SQL-Dialog it asks wether to retrieve just the first 100 results or all results displaying the corect amount of found rows. Or is this just not possible with jdbc.

I'd point out that this does not require knowing exactly how many rows
there are -- only that there are more than 100. You can do the
equivalent in JDBC easily by setting fetchsize to 100 or so and calling
next() 100 times.. if you haven't hit the end of the resultset by that
point, it has more than 100 rows.

-O



view thread (9+ messages)  latest in thread

Message-ID: <42CC5ACE.5070200@opencloud.com>
Permalink:  ../42CC5ACE.5070200@opencloud.com/
Also on:    postgresql.org/message-id/42CC5ACE.5070200@opencloud.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-jdbc@postgresql.org
  Cc: oliver@opencloud.com, mswiege@gmx.de
  Subject: Re: get row count from a "cursor resultset"
  In-Reply-To: <42CC5ACE.5070200@opencloud.com>
  References: <42CC2EB6.8020505@gmx.de> <42CC5ACE.5070200@opencloud.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox