Re: experience sharing: select query returns more records than necessary

From: Oliver Jowett <oliver(at)opencloud(dot)com>
To: Dave Cramer <pg(at)fastcrypt(dot)com>
Cc: Kent Tong <kent(at)cpttm(dot)org(dot)mo>, pgsql-jdbc(at)postgresql(dot)org
Subject: Re: experience sharing: select query returns more records than necessary
Date: 2009-01-20 12:34:18
Message-ID: 4975C4CA.5080302@opencloud.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

Dave Cramer wrote:
>
>
>
> In contrast, if I issue the query on the DB server in the psql
> console, it
> returns records almost immediately. Finally I found that it is
> because the
> postgreSQL JDBC driver is pre-fetching a lot (all?) of the records.
> To fix
> the problem, one can call setFetchSize(50) on the statement.
>
> psql doesnt do anything any differently. It just doesn't have to create
> objects, and you are likely local to the network.

Well, not exactly. The difference is that psql begins outputting as soon
as the first result arrives, while the driver (in non-cursor mode)
gathers the entire resultset before returning anything to the application.

So while the overall query execution time is going to be similar
(excepting object creation, etc), the query *latency* is higher with JDBC.

Though, I would suggest that if you really only care about the first 50
results, then put a LIMIT 50 in your query! The query planner may come
up with a better plan if you do that, too ..

If you want to "stream" the whole resultset, then fetchsize is certainly
the way to do it.

-O

In response to

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message Mario Splivalo 2009-01-20 14:27:09 Pg 8.3, jdbc and UUID
Previous Message Dave Cramer 2009-01-20 12:07:02 Re: experience sharing: select query returns more records than necessary