pg.ddx.io pgsql-jdbc@postgresql.org mailing list archive
help / color / mirror / Atom feedJDBC slow performance on resultSet iteration?
29+ messages / 5 participants
[nested] [flat]
* JDBC slow performance on resultSet iteration?
@ 2013-11-05 16:09 Rui Pedro Leal <rui.pedro.leal@gmail.com>
0 siblings, 1 reply; 29+ messages in thread
From: Rui Pedro Leal @ 2013-11-05 16:09 UTC (permalink / raw)
To: pgsql-jdbc
Hello everyone,
I'm having slow JDBC performance on iterating a resultSet obtained from a somewhat simple query.
The query, although using PostGIS functions, is pretty straight forward:
SELECT _id, _spatial_type, ST_AsBinary(_geometry), _attribute, _count, _references, countpersons, countfatals, countdrunks, density
FROM accidents_5
WHERE (_geometry && ST_MakeEnvelope(-126.60644531250001,26.43122806450644,-63.369140625,52.96187505907603, 4326) )
ORDER BY _pk_id
The results are the following:
- Executing the query directly from pgAdmin: ~2807ms
- Executing from JVM + JDBC: 4184ms
The code i'm executing is pretty much standard:
-------------------- code --------------------
public static void main(final String[] args) throws Exception {
String sql = "SELECT _id, _spatial_type, ST_AsBinary(_geometry), _attribute, _count, _references, countpersons, countfatals, countdrunks, density";
sql += " FROM accidents_5";
sql += " WHERE (_geometry && ST_MakeEnvelope(-126.60644531250001,26.43122806450644,-63.369140625,52.96187505907603, 4326) )";
sql += " ORDER BY _pk_id";
System.out.println(sql);
Long time = System.currentTimeMillis();
try {
Connection connection = DataStoreInfo.getDataStores().get(0);
connection.setAutoCommit(false);
System.out.println("[QUERY " + Thread.currentThread().getId()
+ "] - connection in "
+ (System.currentTimeMillis() - time) + "ms.");
Statement st = connection.createStatement(
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY,
ResultSet.CLOSE_CURSORS_AT_COMMIT);
st.setFetchSize(250);
System.out.println("[QUERY " + Thread.currentThread().getId()
+ "] - statement in " + (System.currentTimeMillis() - time)
+ "ms.");
ResultSet resultSet = st.executeQuery(sql);
System.out.println("[QUERY " + Thread.currentThread().getId()
+ "] - done in " + (System.currentTimeMillis() - time)
+ "ms.");
GeometryFactory geofact = new GeometryFactory(new PrecisionModel(),4326);
WKBReader wkbReader = new WKBReader(geofact);
String id;
String spatialType;
Geometry geometry;
String attribute;
Long count;
String reference;
Map<String, Double> properties;
String granularSynthString = "GranularSynthesis";
while (resultSet.next()) {
id = resultSet.getString(1);
spatialType = resultSet.getString(2);
// geometry = wkbReader.read(resultSet.getBytes(3)); // ignored the WKBReader and the results are about the same.
attribute = resultSet.getString(4);
count = resultSet.getLong(5);
reference = resultSet.getString(6);
properties = new HashMap<String, Double>();
Double aux = resultSet.getDouble(7);
properties.put("countpersons", aux);
aux = resultSet.getDouble(8);
properties.put("countfatals", aux);
aux = resultSet.getDouble(9);
properties.put("countdrunks", aux);
aux = resultSet.getDouble(10);
properties.put("density", aux);
}
System.out.println("[QUERY " + Thread.currentThread().getId()
+ "] - done & iterated in "
+ (System.currentTimeMillis() - time) + "ms.");
resultSet.close();
st.close();
connection.commit();
connection.close();
} catch (SQLException exception) {
exception.printStackTrace();
// } catch (ParseException exception) { // ignored from WKBreader
// exception.printStackTrace();
}
System.out.println("[End " + Thread.currentThread().getId()
+ "] - done in " + (System.currentTimeMillis() - time) + "ms.");
}
-------------------- end code --------------------
Although i'm executing this on slow MacbookPro (2.6 core duo, 2Gbs RAM but SSD) and have a 9.1.2 postgres, i've also tested this on a recent retina MBP and the ratio between pgAdmin and JDBC execution is similar.
Is this expected? Can someone point if i'm doing something terrible wrong?
I'm not concerned about the query performance per-se (i know it CAN be optimized), but the differences just using JDBC and iterating the resultSet are really annoying.
Thanks in advance for any help.
Kind regards,
Rui Leal
--
Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-jdbc
^ permalink raw reply [nested|flat] 29+ messages in thread
* Re: JDBC slow performance on resultSet iteration?
@ 2013-11-05 16:12 Rui Pedro Leal <rui.pedro.leal@gmail.com>
parent: Rui Pedro Leal <rui.pedro.leal@gmail.com>
0 siblings, 1 reply; 29+ messages in thread
From: Rui Pedro Leal @ 2013-11-05 16:12 UTC (permalink / raw)
To: pgsql-jdbc
Just an quick info update: i'm using the postgresql-9.2-1003-jdbc4 driver
On 2013/11/05, at 16:09, Rui Pedro Leal wrote:
> Hello everyone,
>
> I'm having slow JDBC performance on iterating a resultSet obtained from a somewhat simple query.
>
> The query, although using PostGIS functions, is pretty straight forward:
>
> SELECT _id, _spatial_type, ST_AsBinary(_geometry), _attribute, _count, _references, countpersons, countfatals, countdrunks, density
> FROM accidents_5
> WHERE (_geometry && ST_MakeEnvelope(-126.60644531250001,26.43122806450644,-63.369140625,52.96187505907603, 4326) )
> ORDER BY _pk_id
>
> The results are the following:
> - Executing the query directly from pgAdmin: ~2807ms
> - Executing from JVM + JDBC: 4184ms
>
> The code i'm executing is pretty much standard:
>
> -------------------- code --------------------
>
> public static void main(final String[] args) throws Exception {
>
> String sql = "SELECT _id, _spatial_type, ST_AsBinary(_geometry), _attribute, _count, _references, countpersons, countfatals, countdrunks, density";
> sql += " FROM accidents_5";
> sql += " WHERE (_geometry && ST_MakeEnvelope(-126.60644531250001,26.43122806450644,-63.369140625,52.96187505907603, 4326) )";
> sql += " ORDER BY _pk_id";
>
> System.out.println(sql);
>
> Long time = System.currentTimeMillis();
> try {
> Connection connection = DataStoreInfo.getDataStores().get(0);
> connection.setAutoCommit(false);
>
> System.out.println("[QUERY " + Thread.currentThread().getId()
> + "] - connection in "
> + (System.currentTimeMillis() - time) + "ms.");
>
> Statement st = connection.createStatement(
> ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY,
> ResultSet.CLOSE_CURSORS_AT_COMMIT);
> st.setFetchSize(250);
>
> System.out.println("[QUERY " + Thread.currentThread().getId()
> + "] - statement in " + (System.currentTimeMillis() - time)
> + "ms.");
>
> ResultSet resultSet = st.executeQuery(sql);
>
> System.out.println("[QUERY " + Thread.currentThread().getId()
> + "] - done in " + (System.currentTimeMillis() - time)
> + "ms.");
>
> GeometryFactory geofact = new GeometryFactory(new PrecisionModel(),4326);
> WKBReader wkbReader = new WKBReader(geofact);
>
> String id;
> String spatialType;
> Geometry geometry;
> String attribute;
> Long count;
> String reference;
> Map<String, Double> properties;
> String granularSynthString = "GranularSynthesis";
>
> while (resultSet.next()) {
> id = resultSet.getString(1);
> spatialType = resultSet.getString(2);
>
> // geometry = wkbReader.read(resultSet.getBytes(3)); // ignored the WKBReader and the results are about the same.
> attribute = resultSet.getString(4);
> count = resultSet.getLong(5);
> reference = resultSet.getString(6);
>
> properties = new HashMap<String, Double>();
> Double aux = resultSet.getDouble(7);
> properties.put("countpersons", aux);
> aux = resultSet.getDouble(8);
> properties.put("countfatals", aux);
> aux = resultSet.getDouble(9);
> properties.put("countdrunks", aux);
> aux = resultSet.getDouble(10);
> properties.put("density", aux);
> }
>
> System.out.println("[QUERY " + Thread.currentThread().getId()
> + "] - done & iterated in "
> + (System.currentTimeMillis() - time) + "ms.");
>
> resultSet.close();
> st.close();
> connection.commit();
> connection.close();
> } catch (SQLException exception) {
> exception.printStackTrace();
> // } catch (ParseException exception) { // ignored from WKBreader
> // exception.printStackTrace();
> }
>
> System.out.println("[End " + Thread.currentThread().getId()
> + "] - done in " + (System.currentTimeMillis() - time) + "ms.");
> }
>
> -------------------- end code --------------------
>
> Although i'm executing this on slow MacbookPro (2.6 core duo, 2Gbs RAM but SSD) and have a 9.1.2 postgres, i've also tested this on a recent retina MBP and the ratio between pgAdmin and JDBC execution is similar.
>
> Is this expected? Can someone point if i'm doing something terrible wrong?
>
> I'm not concerned about the query performance per-se (i know it CAN be optimized), but the differences just using JDBC and iterating the resultSet are really annoying.
>
> Thanks in advance for any help.
>
> Kind regards,
>
> Rui Leal
>
>
--
Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-jdbc
^ permalink raw reply [nested|flat] 29+ messages in thread
* Re: JDBC slow performance on resultSet iteration?
@ 2013-11-05 16:43 Dave Cramer <pg@fastcrypt.com>
parent: Rui Pedro Leal <rui.pedro.leal@gmail.com>
0 siblings, 1 reply; 29+ messages in thread
From: Dave Cramer @ 2013-11-05 16:43 UTC (permalink / raw)
To: Rui Pedro Leal <rui.pedro.leal@gmail.com>; +Cc: pgsql-jdbc
How can you measure the same query in pgAdmin ? JDBC will use a cursor when
you set the fetchsize.
Dave Cramer
dave.cramer(at)credativ(dot)ca
http://www.credativ.ca
On Tue, Nov 5, 2013 at 11:12 AM, Rui Pedro Leal <rui.pedro.leal@gmail.com>wrote:
> Just an quick info update: i'm using the postgresql-9.2-1003-jdbc4 driver
>
> On 2013/11/05, at 16:09, Rui Pedro Leal wrote:
>
> > Hello everyone,
> >
> > I'm having slow JDBC performance on iterating a resultSet obtained from
> a somewhat simple query.
> >
> > The query, although using PostGIS functions, is pretty straight forward:
> >
> > SELECT _id, _spatial_type, ST_AsBinary(_geometry), _attribute, _count,
> _references, countpersons, countfatals, countdrunks, density
> > FROM accidents_5
> > WHERE (_geometry &&
> ST_MakeEnvelope(-126.60644531250001,26.43122806450644,-63.369140625,52.96187505907603,
> 4326) )
> > ORDER BY _pk_id
> >
> > The results are the following:
> > - Executing the query directly from pgAdmin: ~2807ms
> > - Executing from JVM + JDBC: 4184ms
> >
> > The code i'm executing is pretty much standard:
> >
> > -------------------- code --------------------
> >
> > public static void main(final String[] args) throws Exception {
> >
> > String sql = "SELECT _id, _spatial_type,
> ST_AsBinary(_geometry), _attribute, _count, _references, countpersons,
> countfatals, countdrunks, density";
> > sql += " FROM accidents_5";
> > sql += " WHERE (_geometry &&
> ST_MakeEnvelope(-126.60644531250001,26.43122806450644,-63.369140625,52.96187505907603,
> 4326) )";
> > sql += " ORDER BY _pk_id";
> >
> > System.out.println(sql);
> >
> > Long time = System.currentTimeMillis();
> > try {
> > Connection connection =
> DataStoreInfo.getDataStores().get(0);
> > connection.setAutoCommit(false);
> >
> > System.out.println("[QUERY " +
> Thread.currentThread().getId()
> > + "] - connection in "
> > + (System.currentTimeMillis() -
> time) + "ms.");
> >
> > Statement st = connection.createStatement(
> > ResultSet.TYPE_FORWARD_ONLY,
> ResultSet.CONCUR_READ_ONLY,
> > ResultSet.CLOSE_CURSORS_AT_COMMIT);
> > st.setFetchSize(250);
> >
> > System.out.println("[QUERY " +
> Thread.currentThread().getId()
> > + "] - statement in " +
> (System.currentTimeMillis() - time)
> > + "ms.");
> >
> > ResultSet resultSet = st.executeQuery(sql);
> >
> > System.out.println("[QUERY " +
> Thread.currentThread().getId()
> > + "] - done in " +
> (System.currentTimeMillis() - time)
> > + "ms.");
> >
> > GeometryFactory geofact = new GeometryFactory(new
> PrecisionModel(),4326);
> > WKBReader wkbReader = new WKBReader(geofact);
> >
> > String id;
> > String spatialType;
> > Geometry geometry;
> > String attribute;
> > Long count;
> > String reference;
> > Map<String, Double> properties;
> > String granularSynthString = "GranularSynthesis";
> >
> > while (resultSet.next()) {
> > id = resultSet.getString(1);
> > spatialType = resultSet.getString(2);
> >
> > // geometry =
> wkbReader.read(resultSet.getBytes(3)); // ignored the WKBReader and the
> results are about the same.
> > attribute = resultSet.getString(4);
> > count = resultSet.getLong(5);
> > reference = resultSet.getString(6);
> >
> > properties = new HashMap<String, Double>();
> > Double aux = resultSet.getDouble(7);
> > properties.put("countpersons", aux);
> > aux = resultSet.getDouble(8);
> > properties.put("countfatals", aux);
> > aux = resultSet.getDouble(9);
> > properties.put("countdrunks", aux);
> > aux = resultSet.getDouble(10);
> > properties.put("density", aux);
> > }
> >
> > System.out.println("[QUERY " +
> Thread.currentThread().getId()
> > + "] - done & iterated in "
> > + (System.currentTimeMillis() -
> time) + "ms.");
> >
> > resultSet.close();
> > st.close();
> > connection.commit();
> > connection.close();
> > } catch (SQLException exception) {
> > exception.printStackTrace();
> > // } catch (ParseException exception) { // ignored from
> WKBreader
> > // exception.printStackTrace();
> > }
> >
> > System.out.println("[End " + Thread.currentThread().getId()
> > + "] - done in " +
> (System.currentTimeMillis() - time) + "ms.");
> > }
> >
> > -------------------- end code --------------------
> >
> > Although i'm executing this on slow MacbookPro (2.6 core duo, 2Gbs RAM
> but SSD) and have a 9.1.2 postgres, i've also tested this on a recent
> retina MBP and the ratio between pgAdmin and JDBC execution is similar.
> >
> > Is this expected? Can someone point if i'm doing something terrible
> wrong?
> >
> > I'm not concerned about the query performance per-se (i know it CAN be
> optimized), but the differences just using JDBC and iterating the resultSet
> are really annoying.
> >
> > Thanks in advance for any help.
> >
> > Kind regards,
> >
> > Rui Leal
> >
> >
>
>
>
> --
> Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-jdbc
>
^ permalink raw reply [nested|flat] 29+ messages in thread
* Re: JDBC slow performance on resultSet iteration?
@ 2013-11-05 17:16 Rui Pedro Leal <rui.pedro.leal@gmail.com>
parent: Dave Cramer <pg@fastcrypt.com>
0 siblings, 1 reply; 29+ messages in thread
From: Rui Pedro Leal @ 2013-11-05 17:16 UTC (permalink / raw)
To: Dave Cramer <pg@fastcrypt.com>; +Cc: pgsql-jdbc
Hello Dave, thanks for the quick reply.
I'm just executing the same query on pgAdmin query interface. Just a way to measure performance of the query + getting results vs the JDBC way.
I'm using fetchsize in order to get some faster results when iterating the resultSet. I've tried not to use fetchSize/cursor but the performance is naturally slower (and worser than the simple pgAdmin query).
What i'm asking is .. is this performance penalty i'm getting using postgres JDBC normal and within what's expected? I'm i doing something wrong? Can i do it in any other way?
Regards,
Rui Leal
[] Rui Pedro Leal
[] rui.pedro.leal@gmail.com
On 2013/11/05, at 16:43, Dave Cramer wrote:
> How can you measure the same query in pgAdmin ? JDBC will use a cursor when you set the fetchsize.
>
> Dave Cramer
>
> dave.cramer(at)credativ(dot)ca
> http://www.credativ.ca
>
>
> On Tue, Nov 5, 2013 at 11:12 AM, Rui Pedro Leal <rui.pedro.leal@gmail.com> wrote:
> Just an quick info update: i'm using the postgresql-9.2-1003-jdbc4 driver
>
> On 2013/11/05, at 16:09, Rui Pedro Leal wrote:
>
> > Hello everyone,
> >
> > I'm having slow JDBC performance on iterating a resultSet obtained from a somewhat simple query.
> >
> > The query, although using PostGIS functions, is pretty straight forward:
> >
> > SELECT _id, _spatial_type, ST_AsBinary(_geometry), _attribute, _count, _references, countpersons, countfatals, countdrunks, density
> > FROM accidents_5
> > WHERE (_geometry && ST_MakeEnvelope(-126.60644531250001,26.43122806450644,-63.369140625,52.96187505907603, 4326) )
> > ORDER BY _pk_id
> >
> > The results are the following:
> > - Executing the query directly from pgAdmin: ~2807ms
> > - Executing from JVM + JDBC: 4184ms
> >
> > The code i'm executing is pretty much standard:
> >
> > -------------------- code --------------------
> >
> > public static void main(final String[] args) throws Exception {
> >
> > String sql = "SELECT _id, _spatial_type, ST_AsBinary(_geometry), _attribute, _count, _references, countpersons, countfatals, countdrunks, density";
> > sql += " FROM accidents_5";
> > sql += " WHERE (_geometry && ST_MakeEnvelope(-126.60644531250001,26.43122806450644,-63.369140625,52.96187505907603, 4326) )";
> > sql += " ORDER BY _pk_id";
> >
> > System.out.println(sql);
> >
> > Long time = System.currentTimeMillis();
> > try {
> > Connection connection = DataStoreInfo.getDataStores().get(0);
> > connection.setAutoCommit(false);
> >
> > System.out.println("[QUERY " + Thread.currentThread().getId()
> > + "] - connection in "
> > + (System.currentTimeMillis() - time) + "ms.");
> >
> > Statement st = connection.createStatement(
> > ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY,
> > ResultSet.CLOSE_CURSORS_AT_COMMIT);
> > st.setFetchSize(250);
> >
> > System.out.println("[QUERY " + Thread.currentThread().getId()
> > + "] - statement in " + (System.currentTimeMillis() - time)
> > + "ms.");
> >
> > ResultSet resultSet = st.executeQuery(sql);
> >
> > System.out.println("[QUERY " + Thread.currentThread().getId()
> > + "] - done in " + (System.currentTimeMillis() - time)
> > + "ms.");
> >
> > GeometryFactory geofact = new GeometryFactory(new PrecisionModel(),4326);
> > WKBReader wkbReader = new WKBReader(geofact);
> >
> > String id;
> > String spatialType;
> > Geometry geometry;
> > String attribute;
> > Long count;
> > String reference;
> > Map<String, Double> properties;
> > String granularSynthString = "GranularSynthesis";
> >
> > while (resultSet.next()) {
> > id = resultSet.getString(1);
> > spatialType = resultSet.getString(2);
> >
> > // geometry = wkbReader.read(resultSet.getBytes(3)); // ignored the WKBReader and the results are about the same.
> > attribute = resultSet.getString(4);
> > count = resultSet.getLong(5);
> > reference = resultSet.getString(6);
> >
> > properties = new HashMap<String, Double>();
> > Double aux = resultSet.getDouble(7);
> > properties.put("countpersons", aux);
> > aux = resultSet.getDouble(8);
> > properties.put("countfatals", aux);
> > aux = resultSet.getDouble(9);
> > properties.put("countdrunks", aux);
> > aux = resultSet.getDouble(10);
> > properties.put("density", aux);
> > }
> >
> > System.out.println("[QUERY " + Thread.currentThread().getId()
> > + "] - done & iterated in "
> > + (System.currentTimeMillis() - time) + "ms.");
> >
> > resultSet.close();
> > st.close();
> > connection.commit();
> > connection.close();
> > } catch (SQLException exception) {
> > exception.printStackTrace();
> > // } catch (ParseException exception) { // ignored from WKBreader
> > // exception.printStackTrace();
> > }
> >
> > System.out.println("[End " + Thread.currentThread().getId()
> > + "] - done in " + (System.currentTimeMillis() - time) + "ms.");
> > }
> >
> > -------------------- end code --------------------
> >
> > Although i'm executing this on slow MacbookPro (2.6 core duo, 2Gbs RAM but SSD) and have a 9.1.2 postgres, i've also tested this on a recent retina MBP and the ratio between pgAdmin and JDBC execution is similar.
> >
> > Is this expected? Can someone point if i'm doing something terrible wrong?
> >
> > I'm not concerned about the query performance per-se (i know it CAN be optimized), but the differences just using JDBC and iterating the resultSet are really annoying.
> >
> > Thanks in advance for any help.
> >
> > Kind regards,
> >
> > Rui Leal
> >
> >
>
>
>
> --
> Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-jdbc
>
^ permalink raw reply [nested|flat] 29+ messages in thread
* Re: JDBC slow performance on resultSet iteration?
@ 2013-11-05 17:34 Dave Cramer <pg@fastcrypt.com>
parent: Rui Pedro Leal <rui.pedro.leal@gmail.com>
0 siblings, 2 replies; 29+ messages in thread
From: Dave Cramer @ 2013-11-05 17:34 UTC (permalink / raw)
To: Rui Pedro Leal <rui.pedro.leal@gmail.com>; +Cc: pgsql-jdbc
Rui Leal,
You shouldn't see that much of a penatly, but I question how you are
measuring it. Since you are timing the loop which is using cursors.
Just run a simple query and time it without iterating through the result
set. That would be equivalent
Dave Cramer
dave.cramer(at)credativ(dot)ca
http://www.credativ.ca
On Tue, Nov 5, 2013 at 12:16 PM, Rui Pedro Leal <rui.pedro.leal@gmail.com>wrote:
> Hello Dave, thanks for the quick reply.
>
> I'm just executing the same query on pgAdmin query interface. Just a way
> to measure performance of the query + getting results vs the JDBC way.
>
> I'm using fetchsize in order to get some faster results when iterating the
> resultSet. I've tried not to use fetchSize/cursor but the performance is
> naturally slower (and worser than the simple pgAdmin query).
>
> What i'm asking is .. is this performance penalty i'm getting using
> postgres JDBC normal and within what's expected? I'm i doing something
> wrong? Can i do it in any other way?
>
> Regards,
>
> Rui Leal
>
> [] Rui Pedro Leal
> [] rui.pedro.leal@gmail.com
>
>
> On 2013/11/05, at 16:43, Dave Cramer wrote:
>
> How can you measure the same query in pgAdmin ? JDBC will use a cursor
> when you set the fetchsize.
>
> Dave Cramer
>
> dave.cramer(at)credativ(dot)ca
> http://www.credativ.ca
>
>
> On Tue, Nov 5, 2013 at 11:12 AM, Rui Pedro Leal <rui.pedro.leal@gmail.com>wrote:
>
>> Just an quick info update: i'm using the postgresql-9.2-1003-jdbc4 driver
>>
>> On 2013/11/05, at 16:09, Rui Pedro Leal wrote:
>>
>> > Hello everyone,
>> >
>> > I'm having slow JDBC performance on iterating a resultSet obtained from
>> a somewhat simple query.
>> >
>> > The query, although using PostGIS functions, is pretty straight forward:
>> >
>> > SELECT _id, _spatial_type, ST_AsBinary(_geometry), _attribute, _count,
>> _references, countpersons, countfatals, countdrunks, density
>> > FROM accidents_5
>> > WHERE (_geometry &&
>> ST_MakeEnvelope(-126.60644531250001,26.43122806450644,-63.369140625,52.96187505907603,
>> 4326) )
>> > ORDER BY _pk_id
>> >
>> > The results are the following:
>> > - Executing the query directly from pgAdmin: ~2807ms
>> > - Executing from JVM + JDBC: 4184ms
>> >
>> > The code i'm executing is pretty much standard:
>> >
>> > -------------------- code --------------------
>> >
>> > public static void main(final String[] args) throws Exception {
>> >
>> > String sql = "SELECT _id, _spatial_type,
>> ST_AsBinary(_geometry), _attribute, _count, _references, countpersons,
>> countfatals, countdrunks, density";
>> > sql += " FROM accidents_5";
>> > sql += " WHERE (_geometry &&
>> ST_MakeEnvelope(-126.60644531250001,26.43122806450644,-63.369140625,52.96187505907603,
>> 4326) )";
>> > sql += " ORDER BY _pk_id";
>> >
>> > System.out.println(sql);
>> >
>> > Long time = System.currentTimeMillis();
>> > try {
>> > Connection connection =
>> DataStoreInfo.getDataStores().get(0);
>> > connection.setAutoCommit(false);
>> >
>> > System.out.println("[QUERY " +
>> Thread.currentThread().getId()
>> > + "] - connection in "
>> > + (System.currentTimeMillis() -
>> time) + "ms.");
>> >
>> > Statement st = connection.createStatement(
>> > ResultSet.TYPE_FORWARD_ONLY,
>> ResultSet.CONCUR_READ_ONLY,
>> >
>> ResultSet.CLOSE_CURSORS_AT_COMMIT);
>> > st.setFetchSize(250);
>> >
>> > System.out.println("[QUERY " +
>> Thread.currentThread().getId()
>> > + "] - statement in " +
>> (System.currentTimeMillis() - time)
>> > + "ms.");
>> >
>> > ResultSet resultSet = st.executeQuery(sql);
>> >
>> > System.out.println("[QUERY " +
>> Thread.currentThread().getId()
>> > + "] - done in " +
>> (System.currentTimeMillis() - time)
>> > + "ms.");
>> >
>> > GeometryFactory geofact = new GeometryFactory(new
>> PrecisionModel(),4326);
>> > WKBReader wkbReader = new WKBReader(geofact);
>> >
>> > String id;
>> > String spatialType;
>> > Geometry geometry;
>> > String attribute;
>> > Long count;
>> > String reference;
>> > Map<String, Double> properties;
>> > String granularSynthString = "GranularSynthesis";
>> >
>> > while (resultSet.next()) {
>> > id = resultSet.getString(1);
>> > spatialType = resultSet.getString(2);
>> >
>> > // geometry =
>> wkbReader.read(resultSet.getBytes(3)); // ignored the WKBReader and the
>> results are about the same.
>> > attribute = resultSet.getString(4);
>> > count = resultSet.getLong(5);
>> > reference = resultSet.getString(6);
>> >
>> > properties = new HashMap<String,
>> Double>();
>> > Double aux = resultSet.getDouble(7);
>> > properties.put("countpersons", aux);
>> > aux = resultSet.getDouble(8);
>> > properties.put("countfatals", aux);
>> > aux = resultSet.getDouble(9);
>> > properties.put("countdrunks", aux);
>> > aux = resultSet.getDouble(10);
>> > properties.put("density", aux);
>> > }
>> >
>> > System.out.println("[QUERY " +
>> Thread.currentThread().getId()
>> > + "] - done & iterated in "
>> > + (System.currentTimeMillis() -
>> time) + "ms.");
>> >
>> > resultSet.close();
>> > st.close();
>> > connection.commit();
>> > connection.close();
>> > } catch (SQLException exception) {
>> > exception.printStackTrace();
>> > // } catch (ParseException exception) { // ignored from
>> WKBreader
>> > // exception.printStackTrace();
>> > }
>> >
>> > System.out.println("[End " +
>> Thread.currentThread().getId()
>> > + "] - done in " +
>> (System.currentTimeMillis() - time) + "ms.");
>> > }
>> >
>> > -------------------- end code --------------------
>> >
>> > Although i'm executing this on slow MacbookPro (2.6 core duo, 2Gbs RAM
>> but SSD) and have a 9.1.2 postgres, i've also tested this on a recent
>> retina MBP and the ratio between pgAdmin and JDBC execution is similar.
>> >
>> > Is this expected? Can someone point if i'm doing something terrible
>> wrong?
>> >
>> > I'm not concerned about the query performance per-se (i know it CAN be
>> optimized), but the differences just using JDBC and iterating the resultSet
>> are really annoying.
>> >
>> > Thanks in advance for any help.
>> >
>> > Kind regards,
>> >
>> > Rui Leal
>> >
>> >
>>
>>
>>
>> --
>> Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
>> To make changes to your subscription:
>> http://www.postgresql.org/mailpref/pgsql-jdbc
>>
>
>
>
^ permalink raw reply [nested|flat] 29+ messages in thread
* Re: JDBC slow performance on resultSet iteration?
@ 2013-11-05 17:39 Kevin Wooten <kdubb@me.com>
parent: Dave Cramer <pg@fastcrypt.com>
1 sibling, 2 replies; 29+ messages in thread
From: Kevin Wooten @ 2013-11-05 17:39 UTC (permalink / raw)
To: Dave Cramer <pg@fastcrypt.com>; +Cc: Rui Pedro Leal <rui.pedro.leal@gmail.com>; pgsql-jdbc
Dave,
That is not necessarily true. Most of the drivers data conversion is done in the getXXX calls of the ResultSet. All of which would count as “penalty”. I’m not saying it’s a large penalty, just that this is time that must be considered since pgAdmin is converting and displaying the data as well.
On Nov 5, 2013, at 10:34 AM, Dave Cramer <pg@fastcrypt.com> wrote:
> Rui Leal,
>
> You shouldn't see that much of a penatly, but I question how you are measuring it. Since you are timing the loop which is using cursors.
>
> Just run a simple query and time it without iterating through the result set. That would be equivalent
>
> Dave Cramer
>
> dave.cramer(at)credativ(dot)ca
> http://www.credativ.ca
>
>
> On Tue, Nov 5, 2013 at 12:16 PM, Rui Pedro Leal <rui.pedro.leal@gmail.com> wrote:
> Hello Dave, thanks for the quick reply.
>
> I'm just executing the same query on pgAdmin query interface. Just a way to measure performance of the query + getting results vs the JDBC way.
>
> I'm using fetchsize in order to get some faster results when iterating the resultSet. I've tried not to use fetchSize/cursor but the performance is naturally slower (and worser than the simple pgAdmin query).
>
> What i'm asking is .. is this performance penalty i'm getting using postgres JDBC normal and within what's expected? I'm i doing something wrong? Can i do it in any other way?
>
> Regards,
>
> Rui Leal
>
> [] Rui Pedro Leal
> [] rui.pedro.leal@gmail.com
>
>
> On 2013/11/05, at 16:43, Dave Cramer wrote:
>
>> How can you measure the same query in pgAdmin ? JDBC will use a cursor when you set the fetchsize.
>>
>> Dave Cramer
>>
>> dave.cramer(at)credativ(dot)ca
>> http://www.credativ.ca
>>
>>
>> On Tue, Nov 5, 2013 at 11:12 AM, Rui Pedro Leal <rui.pedro.leal@gmail.com> wrote:
>> Just an quick info update: i'm using the postgresql-9.2-1003-jdbc4 driver
>>
>> On 2013/11/05, at 16:09, Rui Pedro Leal wrote:
>>
>> > Hello everyone,
>> >
>> > I'm having slow JDBC performance on iterating a resultSet obtained from a somewhat simple query.
>> >
>> > The query, although using PostGIS functions, is pretty straight forward:
>> >
>> > SELECT _id, _spatial_type, ST_AsBinary(_geometry), _attribute, _count, _references, countpersons, countfatals, countdrunks, density
>> > FROM accidents_5
>> > WHERE (_geometry && ST_MakeEnvelope(-126.60644531250001,26.43122806450644,-63.369140625,52.96187505907603, 4326) )
>> > ORDER BY _pk_id
>> >
>> > The results are the following:
>> > - Executing the query directly from pgAdmin: ~2807ms
>> > - Executing from JVM + JDBC: 4184ms
>> >
>> > The code i'm executing is pretty much standard:
>> >
>> > -------------------- code --------------------
>> >
>> > public static void main(final String[] args) throws Exception {
>> >
>> > String sql = "SELECT _id, _spatial_type, ST_AsBinary(_geometry), _attribute, _count, _references, countpersons, countfatals, countdrunks, density";
>> > sql += " FROM accidents_5";
>> > sql += " WHERE (_geometry && ST_MakeEnvelope(-126.60644531250001,26.43122806450644,-63.369140625,52.96187505907603, 4326) )";
>> > sql += " ORDER BY _pk_id";
>> >
>> > System.out.println(sql);
>> >
>> > Long time = System.currentTimeMillis();
>> > try {
>> > Connection connection = DataStoreInfo.getDataStores().get(0);
>> > connection.setAutoCommit(false);
>> >
>> > System.out.println("[QUERY " + Thread.currentThread().getId()
>> > + "] - connection in "
>> > + (System.currentTimeMillis() - time) + "ms.");
>> >
>> > Statement st = connection.createStatement(
>> > ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY,
>> > ResultSet.CLOSE_CURSORS_AT_COMMIT);
>> > st.setFetchSize(250);
>> >
>> > System.out.println("[QUERY " + Thread.currentThread().getId()
>> > + "] - statement in " + (System.currentTimeMillis() - time)
>> > + "ms.");
>> >
>> > ResultSet resultSet = st.executeQuery(sql);
>> >
>> > System.out.println("[QUERY " + Thread.currentThread().getId()
>> > + "] - done in " + (System.currentTimeMillis() - time)
>> > + "ms.");
>> >
>> > GeometryFactory geofact = new GeometryFactory(new PrecisionModel(),4326);
>> > WKBReader wkbReader = new WKBReader(geofact);
>> >
>> > String id;
>> > String spatialType;
>> > Geometry geometry;
>> > String attribute;
>> > Long count;
>> > String reference;
>> > Map<String, Double> properties;
>> > String granularSynthString = "GranularSynthesis";
>> >
>> > while (resultSet.next()) {
>> > id = resultSet.getString(1);
>> > spatialType = resultSet.getString(2);
>> >
>> > // geometry = wkbReader.read(resultSet.getBytes(3)); // ignored the WKBReader and the results are about the same.
>> > attribute = resultSet.getString(4);
>> > count = resultSet.getLong(5);
>> > reference = resultSet.getString(6);
>> >
>> > properties = new HashMap<String, Double>();
>> > Double aux = resultSet.getDouble(7);
>> > properties.put("countpersons", aux);
>> > aux = resultSet.getDouble(8);
>> > properties.put("countfatals", aux);
>> > aux = resultSet.getDouble(9);
>> > properties.put("countdrunks", aux);
>> > aux = resultSet.getDouble(10);
>> > properties.put("density", aux);
>> > }
>> >
>> > System.out.println("[QUERY " + Thread.currentThread().getId()
>> > + "] - done & iterated in "
>> > + (System.currentTimeMillis() - time) + "ms.");
>> >
>> > resultSet.close();
>> > st.close();
>> > connection.commit();
>> > connection.close();
>> > } catch (SQLException exception) {
>> > exception.printStackTrace();
>> > // } catch (ParseException exception) { // ignored from WKBreader
>> > // exception.printStackTrace();
>> > }
>> >
>> > System.out.println("[End " + Thread.currentThread().getId()
>> > + "] - done in " + (System.currentTimeMillis() - time) + "ms.");
>> > }
>> >
>> > -------------------- end code --------------------
>> >
>> > Although i'm executing this on slow MacbookPro (2.6 core duo, 2Gbs RAM but SSD) and have a 9.1.2 postgres, i've also tested this on a recent retina MBP and the ratio between pgAdmin and JDBC execution is similar.
>> >
>> > Is this expected? Can someone point if i'm doing something terrible wrong?
>> >
>> > I'm not concerned about the query performance per-se (i know it CAN be optimized), but the differences just using JDBC and iterating the resultSet are really annoying.
>> >
>> > Thanks in advance for any help.
>> >
>> > Kind regards,
>> >
>> > Rui Leal
>> >
>> >
>>
>>
>>
>> --
>> Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
>> To make changes to your subscription:
>> http://www.postgresql.org/mailpref/pgsql-jdbc
>>
>
>
^ permalink raw reply [nested|flat] 29+ messages in thread
* Re: JDBC slow performance on resultSet iteration?
@ 2013-11-05 17:42 Dave Cramer <pg@fastcrypt.com>
parent: Kevin Wooten <kdubb@me.com>
1 sibling, 1 reply; 29+ messages in thread
From: Dave Cramer @ 2013-11-05 17:42 UTC (permalink / raw)
To: Kevin Wooten <kdubb@me.com>; +Cc: Rui Pedro Leal <rui.pedro.leal@gmail.com>; pgsql-jdbc
Kevin,
Ya, however I am still questioning the basic time measurement.
Dave Cramer
dave.cramer(at)credativ(dot)ca
http://www.credativ.ca
On Tue, Nov 5, 2013 at 12:39 PM, Kevin Wooten <kdubb@me.com> wrote:
> Dave,
> That is not necessarily true. Most of the drivers data conversion is done
> in the getXXX calls of the ResultSet. All of which would count as
> “penalty”. I’m not saying it’s a large penalty, just that this is time
> that must be considered since pgAdmin is converting and displaying the data
> as well.
>
> On Nov 5, 2013, at 10:34 AM, Dave Cramer <pg@fastcrypt.com> wrote:
>
> Rui Leal,
>
> You shouldn't see that much of a penatly, but I question how you are
> measuring it. Since you are timing the loop which is using cursors.
>
> Just run a simple query and time it without iterating through the result
> set. That would be equivalent
>
> Dave Cramer
>
> dave.cramer(at)credativ(dot)ca
> http://www.credativ.ca
>
>
> On Tue, Nov 5, 2013 at 12:16 PM, Rui Pedro Leal <rui.pedro.leal@gmail.com>wrote:
>
>> Hello Dave, thanks for the quick reply.
>>
>> I'm just executing the same query on pgAdmin query interface. Just a way
>> to measure performance of the query + getting results vs the JDBC way.
>>
>> I'm using fetchsize in order to get some faster results when iterating
>> the resultSet. I've tried not to use fetchSize/cursor but the performance
>> is naturally slower (and worser than the simple pgAdmin query).
>>
>> What i'm asking is .. is this performance penalty i'm getting using
>> postgres JDBC normal and within what's expected? I'm i doing something
>> wrong? Can i do it in any other way?
>>
>> Regards,
>>
>> Rui Leal
>>
>> [] Rui Pedro Leal
>> [] rui.pedro.leal@gmail.com
>>
>>
>> On 2013/11/05, at 16:43, Dave Cramer wrote:
>>
>> How can you measure the same query in pgAdmin ? JDBC will use a cursor
>> when you set the fetchsize.
>>
>> Dave Cramer
>>
>> dave.cramer(at)credativ(dot)ca
>> http://www.credativ.ca
>>
>>
>> On Tue, Nov 5, 2013 at 11:12 AM, Rui Pedro Leal <rui.pedro.leal@gmail.com
>> > wrote:
>>
>>> Just an quick info update: i'm using the postgresql-9.2-1003-jdbc4 driver
>>>
>>> On 2013/11/05, at 16:09, Rui Pedro Leal wrote:
>>>
>>> > Hello everyone,
>>> >
>>> > I'm having slow JDBC performance on iterating a resultSet obtained
>>> from a somewhat simple query.
>>> >
>>> > The query, although using PostGIS functions, is pretty straight
>>> forward:
>>> >
>>> > SELECT _id, _spatial_type, ST_AsBinary(_geometry), _attribute, _count,
>>> _references, countpersons, countfatals, countdrunks, density
>>> > FROM accidents_5
>>> > WHERE (_geometry &&
>>> ST_MakeEnvelope(-126.60644531250001,26.43122806450644,-63.369140625,52.96187505907603,
>>> 4326) )
>>> > ORDER BY _pk_id
>>> >
>>> > The results are the following:
>>> > - Executing the query directly from pgAdmin: ~2807ms
>>> > - Executing from JVM + JDBC: 4184ms
>>> >
>>> > The code i'm executing is pretty much standard:
>>> >
>>> > -------------------- code --------------------
>>> >
>>> > public static void main(final String[] args) throws Exception {
>>> >
>>> > String sql = "SELECT _id, _spatial_type,
>>> ST_AsBinary(_geometry), _attribute, _count, _references, countpersons,
>>> countfatals, countdrunks, density";
>>> > sql += " FROM accidents_5";
>>> > sql += " WHERE (_geometry &&
>>> ST_MakeEnvelope(-126.60644531250001,26.43122806450644,-63.369140625,52.96187505907603,
>>> 4326) )";
>>> > sql += " ORDER BY _pk_id";
>>> >
>>> > System.out.println(sql);
>>> >
>>> > Long time = System.currentTimeMillis();
>>> > try {
>>> > Connection connection =
>>> DataStoreInfo.getDataStores().get(0);
>>> > connection.setAutoCommit(false);
>>> >
>>> > System.out.println("[QUERY " +
>>> Thread.currentThread().getId()
>>> > + "] - connection in "
>>> > + (System.currentTimeMillis() -
>>> time) + "ms.");
>>> >
>>> > Statement st = connection.createStatement(
>>> > ResultSet.TYPE_FORWARD_ONLY,
>>> ResultSet.CONCUR_READ_ONLY,
>>> >
>>> ResultSet.CLOSE_CURSORS_AT_COMMIT);
>>> > st.setFetchSize(250);
>>> >
>>> > System.out.println("[QUERY " +
>>> Thread.currentThread().getId()
>>> > + "] - statement in " +
>>> (System.currentTimeMillis() - time)
>>> > + "ms.");
>>> >
>>> > ResultSet resultSet = st.executeQuery(sql);
>>> >
>>> > System.out.println("[QUERY " +
>>> Thread.currentThread().getId()
>>> > + "] - done in " +
>>> (System.currentTimeMillis() - time)
>>> > + "ms.");
>>> >
>>> > GeometryFactory geofact = new
>>> GeometryFactory(new PrecisionModel(),4326);
>>> > WKBReader wkbReader = new WKBReader(geofact);
>>> >
>>> > String id;
>>> > String spatialType;
>>> > Geometry geometry;
>>> > String attribute;
>>> > Long count;
>>> > String reference;
>>> > Map<String, Double> properties;
>>> > String granularSynthString = "GranularSynthesis";
>>> >
>>> > while (resultSet.next()) {
>>> > id = resultSet.getString(1);
>>> > spatialType = resultSet.getString(2);
>>> >
>>> > // geometry =
>>> wkbReader.read(resultSet.getBytes(3)); // ignored the WKBReader and the
>>> results are about the same.
>>> > attribute = resultSet.getString(4);
>>> > count = resultSet.getLong(5);
>>> > reference = resultSet.getString(6);
>>> >
>>> > properties = new HashMap<String,
>>> Double>();
>>> > Double aux = resultSet.getDouble(7);
>>> > properties.put("countpersons", aux);
>>> > aux = resultSet.getDouble(8);
>>> > properties.put("countfatals", aux);
>>> > aux = resultSet.getDouble(9);
>>> > properties.put("countdrunks", aux);
>>> > aux = resultSet.getDouble(10);
>>> > properties.put("density", aux);
>>> > }
>>> >
>>> > System.out.println("[QUERY " +
>>> Thread.currentThread().getId()
>>> > + "] - done & iterated in "
>>> > + (System.currentTimeMillis() -
>>> time) + "ms.");
>>> >
>>> > resultSet.close();
>>> > st.close();
>>> > connection.commit();
>>> > connection.close();
>>> > } catch (SQLException exception) {
>>> > exception.printStackTrace();
>>> > // } catch (ParseException exception) { // ignored from
>>> WKBreader
>>> > // exception.printStackTrace();
>>> > }
>>> >
>>> > System.out.println("[End " +
>>> Thread.currentThread().getId()
>>> > + "] - done in " +
>>> (System.currentTimeMillis() - time) + "ms.");
>>> > }
>>> >
>>> > -------------------- end code --------------------
>>> >
>>> > Although i'm executing this on slow MacbookPro (2.6 core duo, 2Gbs RAM
>>> but SSD) and have a 9.1.2 postgres, i've also tested this on a recent
>>> retina MBP and the ratio between pgAdmin and JDBC execution is similar.
>>> >
>>> > Is this expected? Can someone point if i'm doing something terrible
>>> wrong?
>>> >
>>> > I'm not concerned about the query performance per-se (i know it CAN be
>>> optimized), but the differences just using JDBC and iterating the resultSet
>>> are really annoying.
>>> >
>>> > Thanks in advance for any help.
>>> >
>>> > Kind regards,
>>> >
>>> > Rui Leal
>>> >
>>> >
>>>
>>>
>>>
>>> --
>>> Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
>>> To make changes to your subscription:
>>> http://www.postgresql.org/mailpref/pgsql-jdbc
>>>
>>
>>
>>
>
>
^ permalink raw reply [nested|flat] 29+ messages in thread
* Re: JDBC slow performance on resultSet iteration?
@ 2013-11-05 17:47 Edson Richter <edsonrichter@hotmail.com>
parent: Dave Cramer <pg@fastcrypt.com>
0 siblings, 1 reply; 29+ messages in thread
From: Edson Richter @ 2013-11-05 17:47 UTC (permalink / raw)
To: pgsql-jdbc
Em 05/11/2013 15:42, Dave Cramer escreveu:
> Kevin,
>
> Ya, however I am still questioning the basic time measurement.
>
> Dave Cramer
>
> dave.cramer(at)credativ(dot)ca
> http://www.credativ.ca
>
>
> On Tue, Nov 5, 2013 at 12:39 PM, Kevin Wooten <kdubb@me.com
> <mailto:kdubb@me.com>> wrote:
>
> Dave,
> That is not necessarily true. Most of the drivers data conversion
> is done in the getXXX calls of the ResultSet. All of which would
> count as "penalty". I'm not saying it's a large penalty, just
> that this is time that must be considered since pgAdmin is
> converting and displaying the data as well.
>
> On Nov 5, 2013, at 10:34 AM, Dave Cramer <pg@fastcrypt.com
> <mailto:pg@fastcrypt.com>> wrote:
>
>> Rui Leal,
>>
>> You shouldn't see that much of a penatly, but I question how you
>> are measuring it. Since you are timing the loop which is using
>> cursors.
>>
>> Just run a simple query and time it without iterating through the
>> result set. That would be equivalent
>>
>> Dave Cramer
>>
>> dave.cramer(at)credativ(dot)ca
>> http://www.credativ.ca <http://www.credativ.ca/;
>>
>>
>> On Tue, Nov 5, 2013 at 12:16 PM, Rui Pedro Leal
>> <rui.pedro.leal@gmail.com <mailto:rui.pedro.leal@gmail.com>> wrote:
>>
>> Hello Dave, thanks for the quick reply.
>>
>> I'm just executing the same query on pgAdmin query interface.
>> Just a way to measure performance of the query + getting
>> results vs the JDBC way.
>>
>> I'm using fetchsize in order to get some faster results when
>> iterating the resultSet. I've tried not to use
>> fetchSize/cursor but the performance is naturally slower (and
>> worser than the simple pgAdmin query).
>>
>> What i'm asking is .. is this performance penalty i'm getting
>> using postgres JDBC normal and within what's expected? I'm i
>> doing something wrong? Can i do it in any other way?
>>
>> Regards,
>>
>> Rui Leal
>>
>> [] Rui Pedro Leal
>> [] rui.pedro.leal@gmail.com <mailto:rui.pedro.leal@gmail.com>
>>
>>
>> On 2013/11/05, at 16:43, Dave Cramer wrote:
>>
>>> How can you measure the same query in pgAdmin ? JDBC will
>>> use a cursor when you set the fetchsize.
>>>
>>> Dave Cramer
>>>
>>> dave.cramer(at)credativ(dot)ca
>>> http://www.credativ.ca <http://www.credativ.ca/;
>>>
>>>
>>> On Tue, Nov 5, 2013 at 11:12 AM, Rui Pedro Leal
>>> <rui.pedro.leal@gmail.com <mailto:rui.pedro.leal@gmail.com>>
>>> wrote:
>>>
>>> Just an quick info update: i'm using the
>>> postgresql-9.2-1003-jdbc4 driver
>>>
>>> On 2013/11/05, at 16:09, Rui Pedro Leal wrote:
>>>
>>> > Hello everyone,
>>> >
>>> > I'm having slow JDBC performance on iterating a
>>> resultSet obtained from a somewhat simple query.
>>> >
>>> > The query, although using PostGIS functions, is pretty
>>> straight forward:
>>> >
>>> > SELECT _id, _spatial_type, ST_AsBinary(_geometry),
>>> _attribute, _count, _references, countpersons,
>>> countfatals, countdrunks, density
>>> > FROM accidents_5
>>> > WHERE (_geometry &&
>>> ST_MakeEnvelope(-126.60644531250001,26.43122806450644,-63.369140625,52.96187505907603,
>>> 4326) )
>>> > ORDER BY _pk_id
>>> >
>>> > The results are the following:
>>> > - Executing the query directly from pgAdmin: ~2807ms
>>> > - Executing from JVM + JDBC: 4184ms
>>> >
>>> > The code i'm executing is pretty much standard:
>>> >
>>> > -------------------- code --------------------
>>> >
>>> > public static void main(final String[] args)
>>> throws Exception {
>>> >
>>> > String sql = "SELECT _id, _spatial_type,
>>> ST_AsBinary(_geometry), _attribute, _count, _references,
>>> countpersons, countfatals, countdrunks, density";
>>> > sql += " FROM accidents_5";
>>> > sql += " WHERE (_geometry &&
>>> ST_MakeEnvelope(-126.60644531250001,26.43122806450644,-63.369140625,52.96187505907603,
>>> 4326) )";
>>> > sql += " ORDER BY _pk_id";
>>> >
>>> > System.out.println(sql);
>>> >
>>> > Long time = System.currentTimeMillis();
>>> > try {
>>> > Connection connection =
>>> DataStoreInfo.getDataStores().get(0);
>>> > connection.setAutoCommit(false);
>>> >
>>> > System.out.println("[QUERY " +
>>> Thread.currentThread().getId()
>>> > + "] - connection in "
>>> > + (System.currentTimeMillis() -
>>> time) + "ms.");
>>> >
>>> > Statement st = connection.createStatement(
>>> > ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY,
>>> > ResultSet.CLOSE_CURSORS_AT_COMMIT);
>>> > st.setFetchSize(250);
>>> >
>>> > System.out.println("[QUERY " +
>>> Thread.currentThread().getId()
>>> > + "] - statement in " +
>>> (System.currentTimeMillis() - time)
>>> > + "ms.");
>>> >
>>> > ResultSet resultSet = st.executeQuery(sql);
>>> >
>>> > System.out.println("[QUERY " +
>>> Thread.currentThread().getId()
>>> > + "] - done in " +
>>> (System.currentTimeMillis() - time)
>>> > + "ms.");
>>> >
>>> > GeometryFactory geofact = new GeometryFactory(new
>>> PrecisionModel(),4326);
>>> > WKBReader wkbReader = new WKBReader(geofact);
>>> >
>>> > String id;
>>> > String spatialType;
>>> > Geometry geometry;
>>> > String attribute;
>>> > Long count;
>>> > String reference;
>>> > Map<String, Double> properties;
>>> > String granularSynthString = "GranularSynthesis";
>>> >
>>> > while (resultSet.next()) {
>>> > id = resultSet.getString(1);
>>> > spatialType = resultSet.getString(2);
>>> >
>>> > // geometry =
>>> wkbReader.read(resultSet.getBytes(3)); // ignored the
>>> WKBReader and the results are about the same.
>>> > attribute = resultSet.getString(4);
>>> > count = resultSet.getLong(5);
>>> > reference = resultSet.getString(6);
>>> >
>>> > properties = new HashMap<String, Double>();
>>> > Double aux = resultSet.getDouble(7);
>>> > properties.put("countpersons", aux);
>>> > aux = resultSet.getDouble(8);
>>> > properties.put("countfatals", aux);
>>> > aux = resultSet.getDouble(9);
>>> > properties.put("countdrunks", aux);
>>> > aux = resultSet.getDouble(10);
>>> > properties.put("density", aux);
>>> > }
>>> >
>>> > System.out.println("[QUERY " +
>>> Thread.currentThread().getId()
>>> > + "] - done & iterated in "
>>> > + (System.currentTimeMillis() -
>>> time) + "ms.");
>>> >
>>> > resultSet.close();
>>> > st.close();
>>> > connection.commit();
>>> > connection.close();
>>> > } catch (SQLException exception) {
>>> > exception.printStackTrace();
>>> > // } catch (ParseException exception) {
>>> // ignored from WKBreader
>>> > // exception.printStackTrace();
>>> > }
>>> >
>>> > System.out.println("[End " +
>>> Thread.currentThread().getId()
>>> > + "] - done in " +
>>> (System.currentTimeMillis() - time) + "ms.");
>>> > }
>>> >
>>> > -------------------- end code --------------------
>>> >
>>> > Although i'm executing this on slow MacbookPro (2.6
>>> core duo, 2Gbs RAM but SSD) and have a 9.1.2 postgres,
>>> i've also tested this on a recent retina MBP and the
>>> ratio between pgAdmin and JDBC execution is similar.
>>> >
>>> > Is this expected? Can someone point if i'm doing
>>> something terrible wrong?
>>> >
>>> > I'm not concerned about the query performance per-se
>>> (i know it CAN be optimized), but the differences just
>>> using JDBC and iterating the resultSet are really annoying.
>>> >
>>> > Thanks in advance for any help.
>>> >
>>> > Kind regards,
>>> >
>>> > Rui Leal
>>> >
>>> >
>>>
>>>
>>>
>>> --
>>> Sent via pgsql-jdbc mailing list
>>> (pgsql-jdbc@postgresql.org
>>> <mailto:pgsql-jdbc@postgresql.org>)
>>> To make changes to your subscription:
>>> http://www.postgresql.org/mailpref/pgsql-jdbc
>>>
>>>
>>
>>
>
>
Are you using a network between your station and the server?
You're connection using IP address or server name?
If the client machine has to access server several times (like in a
cursor scenario), would be necessary to solve machine names, which could
slow down the whole process.
Regards,
Edson
^ permalink raw reply [nested|flat] 29+ messages in thread
* Re: JDBC slow performance on resultSet iteration?
@ 2013-11-05 18:00 Rui Pedro Leal <rui.pedro.leal@gmail.com>
parent: Dave Cramer <pg@fastcrypt.com>
1 sibling, 1 reply; 29+ messages in thread
From: Rui Pedro Leal @ 2013-11-05 18:00 UTC (permalink / raw)
To: Dave Cramer <pg@fastcrypt.com>; +Cc: pgsql-jdbc
Dave,
I understand what you are saying, but i must get the results, right? :)
I think in pgAdmin the total query time, presented by the query execution windows, is also considering the rendering / presenting of the query results, so this is comparable ... right?
[] Rui Pedro Leal
[] rui.pedro.leal@gmail.com
On 2013/11/05, at 17:34, Dave Cramer wrote:
> Rui Leal,
>
> You shouldn't see that much of a penatly, but I question how you are measuring it. Since you are timing the loop which is using cursors.
>
> Just run a simple query and time it without iterating through the result set. That would be equivalent
>
> Dave Cramer
>
> dave.cramer(at)credativ(dot)ca
> http://www.credativ.ca
>
>
> On Tue, Nov 5, 2013 at 12:16 PM, Rui Pedro Leal <rui.pedro.leal@gmail.com> wrote:
> Hello Dave, thanks for the quick reply.
>
> I'm just executing the same query on pgAdmin query interface. Just a way to measure performance of the query + getting results vs the JDBC way.
>
> I'm using fetchsize in order to get some faster results when iterating the resultSet. I've tried not to use fetchSize/cursor but the performance is naturally slower (and worser than the simple pgAdmin query).
>
> What i'm asking is .. is this performance penalty i'm getting using postgres JDBC normal and within what's expected? I'm i doing something wrong? Can i do it in any other way?
>
> Regards,
>
> Rui Leal
>
> [] Rui Pedro Leal
> [] rui.pedro.leal@gmail.com
>
>
> On 2013/11/05, at 16:43, Dave Cramer wrote:
>
>> How can you measure the same query in pgAdmin ? JDBC will use a cursor when you set the fetchsize.
>>
>> Dave Cramer
>>
>> dave.cramer(at)credativ(dot)ca
>> http://www.credativ.ca
>>
>>
>> On Tue, Nov 5, 2013 at 11:12 AM, Rui Pedro Leal <rui.pedro.leal@gmail.com> wrote:
>> Just an quick info update: i'm using the postgresql-9.2-1003-jdbc4 driver
>>
>> On 2013/11/05, at 16:09, Rui Pedro Leal wrote:
>>
>> > Hello everyone,
>> >
>> > I'm having slow JDBC performance on iterating a resultSet obtained from a somewhat simple query.
>> >
>> > The query, although using PostGIS functions, is pretty straight forward:
>> >
>> > SELECT _id, _spatial_type, ST_AsBinary(_geometry), _attribute, _count, _references, countpersons, countfatals, countdrunks, density
>> > FROM accidents_5
>> > WHERE (_geometry && ST_MakeEnvelope(-126.60644531250001,26.43122806450644,-63.369140625,52.96187505907603, 4326) )
>> > ORDER BY _pk_id
>> >
>> > The results are the following:
>> > - Executing the query directly from pgAdmin: ~2807ms
>> > - Executing from JVM + JDBC: 4184ms
>> >
>> > The code i'm executing is pretty much standard:
>> >
>> > -------------------- code --------------------
>> >
>> > public static void main(final String[] args) throws Exception {
>> >
>> > String sql = "SELECT _id, _spatial_type, ST_AsBinary(_geometry), _attribute, _count, _references, countpersons, countfatals, countdrunks, density";
>> > sql += " FROM accidents_5";
>> > sql += " WHERE (_geometry && ST_MakeEnvelope(-126.60644531250001,26.43122806450644,-63.369140625,52.96187505907603, 4326) )";
>> > sql += " ORDER BY _pk_id";
>> >
>> > System.out.println(sql);
>> >
>> > Long time = System.currentTimeMillis();
>> > try {
>> > Connection connection = DataStoreInfo.getDataStores().get(0);
>> > connection.setAutoCommit(false);
>> >
>> > System.out.println("[QUERY " + Thread.currentThread().getId()
>> > + "] - connection in "
>> > + (System.currentTimeMillis() - time) + "ms.");
>> >
>> > Statement st = connection.createStatement(
>> > ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY,
>> > ResultSet.CLOSE_CURSORS_AT_COMMIT);
>> > st.setFetchSize(250);
>> >
>> > System.out.println("[QUERY " + Thread.currentThread().getId()
>> > + "] - statement in " + (System.currentTimeMillis() - time)
>> > + "ms.");
>> >
>> > ResultSet resultSet = st.executeQuery(sql);
>> >
>> > System.out.println("[QUERY " + Thread.currentThread().getId()
>> > + "] - done in " + (System.currentTimeMillis() - time)
>> > + "ms.");
>> >
>> > GeometryFactory geofact = new GeometryFactory(new PrecisionModel(),4326);
>> > WKBReader wkbReader = new WKBReader(geofact);
>> >
>> > String id;
>> > String spatialType;
>> > Geometry geometry;
>> > String attribute;
>> > Long count;
>> > String reference;
>> > Map<String, Double> properties;
>> > String granularSynthString = "GranularSynthesis";
>> >
>> > while (resultSet.next()) {
>> > id = resultSet.getString(1);
>> > spatialType = resultSet.getString(2);
>> >
>> > // geometry = wkbReader.read(resultSet.getBytes(3)); // ignored the WKBReader and the results are about the same.
>> > attribute = resultSet.getString(4);
>> > count = resultSet.getLong(5);
>> > reference = resultSet.getString(6);
>> >
>> > properties = new HashMap<String, Double>();
>> > Double aux = resultSet.getDouble(7);
>> > properties.put("countpersons", aux);
>> > aux = resultSet.getDouble(8);
>> > properties.put("countfatals", aux);
>> > aux = resultSet.getDouble(9);
>> > properties.put("countdrunks", aux);
>> > aux = resultSet.getDouble(10);
>> > properties.put("density", aux);
>> > }
>> >
>> > System.out.println("[QUERY " + Thread.currentThread().getId()
>> > + "] - done & iterated in "
>> > + (System.currentTimeMillis() - time) + "ms.");
>> >
>> > resultSet.close();
>> > st.close();
>> > connection.commit();
>> > connection.close();
>> > } catch (SQLException exception) {
>> > exception.printStackTrace();
>> > // } catch (ParseException exception) { // ignored from WKBreader
>> > // exception.printStackTrace();
>> > }
>> >
>> > System.out.println("[End " + Thread.currentThread().getId()
>> > + "] - done in " + (System.currentTimeMillis() - time) + "ms.");
>> > }
>> >
>> > -------------------- end code --------------------
>> >
>> > Although i'm executing this on slow MacbookPro (2.6 core duo, 2Gbs RAM but SSD) and have a 9.1.2 postgres, i've also tested this on a recent retina MBP and the ratio between pgAdmin and JDBC execution is similar.
>> >
>> > Is this expected? Can someone point if i'm doing something terrible wrong?
>> >
>> > I'm not concerned about the query performance per-se (i know it CAN be optimized), but the differences just using JDBC and iterating the resultSet are really annoying.
>> >
>> > Thanks in advance for any help.
>> >
>> > Kind regards,
>> >
>> > Rui Leal
>> >
>> >
>>
>>
>>
>> --
>> Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
>> To make changes to your subscription:
>> http://www.postgresql.org/mailpref/pgsql-jdbc
>>
>
>
^ permalink raw reply [nested|flat] 29+ messages in thread
* Re: JDBC slow performance on resultSet iteration?
@ 2013-11-05 18:02 Rui Pedro Leal <rui.pedro.leal@gmail.com>
parent: Kevin Wooten <kdubb@me.com>
1 sibling, 0 replies; 29+ messages in thread
From: Rui Pedro Leal @ 2013-11-05 18:02 UTC (permalink / raw)
To: Kevin Wooten <kdubb@me.com>; +Cc: Dave Cramer <pg@fastcrypt.com>; pgsql-jdbc
Kevin,
Exactly. I'm executing the query and presenting the results in pgAdmin. I assume pgAdmin is iterating through an equivalent of resultSet, getting each column, and presenting it in the results window.
On 2013/11/05, at 17:39, Kevin Wooten wrote:
> Dave,
> That is not necessarily true. Most of the drivers data conversion is done in the getXXX calls of the ResultSet. All of which would count as “penalty”. I’m not saying it’s a large penalty, just that this is time that must be considered since pgAdmin is converting and displaying the data as well.
>
> On Nov 5, 2013, at 10:34 AM, Dave Cramer <pg@fastcrypt.com> wrote:
>
>> Rui Leal,
>>
>> You shouldn't see that much of a penatly, but I question how you are measuring it. Since you are timing the loop which is using cursors.
>>
>> Just run a simple query and time it without iterating through the result set. That would be equivalent
>>
>> Dave Cramer
>>
>> dave.cramer(at)credativ(dot)ca
>> http://www.credativ.ca
>>
>>
>> On Tue, Nov 5, 2013 at 12:16 PM, Rui Pedro Leal <rui.pedro.leal@gmail.com> wrote:
>> Hello Dave, thanks for the quick reply.
>>
>> I'm just executing the same query on pgAdmin query interface. Just a way to measure performance of the query + getting results vs the JDBC way.
>>
>> I'm using fetchsize in order to get some faster results when iterating the resultSet. I've tried not to use fetchSize/cursor but the performance is naturally slower (and worser than the simple pgAdmin query).
>>
>> What i'm asking is .. is this performance penalty i'm getting using postgres JDBC normal and within what's expected? I'm i doing something wrong? Can i do it in any other way?
>>
>> Regards,
>>
>> Rui Leal
>>
>> [] Rui Pedro Leal
>> [] rui.pedro.leal@gmail.com
>>
>>
>> On 2013/11/05, at 16:43, Dave Cramer wrote:
>>
>>> How can you measure the same query in pgAdmin ? JDBC will use a cursor when you set the fetchsize.
>>>
>>> Dave Cramer
>>>
>>> dave.cramer(at)credativ(dot)ca
>>> http://www.credativ.ca
>>>
>>>
>>> On Tue, Nov 5, 2013 at 11:12 AM, Rui Pedro Leal <rui.pedro.leal@gmail.com> wrote:
>>> Just an quick info update: i'm using the postgresql-9.2-1003-jdbc4 driver
>>>
>>> On 2013/11/05, at 16:09, Rui Pedro Leal wrote:
>>>
>>> > Hello everyone,
>>> >
>>> > I'm having slow JDBC performance on iterating a resultSet obtained from a somewhat simple query.
>>> >
>>> > The query, although using PostGIS functions, is pretty straight forward:
>>> >
>>> > SELECT _id, _spatial_type, ST_AsBinary(_geometry), _attribute, _count, _references, countpersons, countfatals, countdrunks, density
>>> > FROM accidents_5
>>> > WHERE (_geometry && ST_MakeEnvelope(-126.60644531250001,26.43122806450644,-63.369140625,52.96187505907603, 4326) )
>>> > ORDER BY _pk_id
>>> >
>>> > The results are the following:
>>> > - Executing the query directly from pgAdmin: ~2807ms
>>> > - Executing from JVM + JDBC: 4184ms
>>> >
>>> > The code i'm executing is pretty much standard:
>>> >
>>> > -------------------- code --------------------
>>> >
>>> > public static void main(final String[] args) throws Exception {
>>> >
>>> > String sql = "SELECT _id, _spatial_type, ST_AsBinary(_geometry), _attribute, _count, _references, countpersons, countfatals, countdrunks, density";
>>> > sql += " FROM accidents_5";
>>> > sql += " WHERE (_geometry && ST_MakeEnvelope(-126.60644531250001,26.43122806450644,-63.369140625,52.96187505907603, 4326) )";
>>> > sql += " ORDER BY _pk_id";
>>> >
>>> > System.out.println(sql);
>>> >
>>> > Long time = System.currentTimeMillis();
>>> > try {
>>> > Connection connection = DataStoreInfo.getDataStores().get(0);
>>> > connection.setAutoCommit(false);
>>> >
>>> > System.out.println("[QUERY " + Thread.currentThread().getId()
>>> > + "] - connection in "
>>> > + (System.currentTimeMillis() - time) + "ms.");
>>> >
>>> > Statement st = connection.createStatement(
>>> > ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY,
>>> > ResultSet.CLOSE_CURSORS_AT_COMMIT);
>>> > st.setFetchSize(250);
>>> >
>>> > System.out.println("[QUERY " + Thread.currentThread().getId()
>>> > + "] - statement in " + (System.currentTimeMillis() - time)
>>> > + "ms.");
>>> >
>>> > ResultSet resultSet = st.executeQuery(sql);
>>> >
>>> > System.out.println("[QUERY " + Thread.currentThread().getId()
>>> > + "] - done in " + (System.currentTimeMillis() - time)
>>> > + "ms.");
>>> >
>>> > GeometryFactory geofact = new GeometryFactory(new PrecisionModel(),4326);
>>> > WKBReader wkbReader = new WKBReader(geofact);
>>> >
>>> > String id;
>>> > String spatialType;
>>> > Geometry geometry;
>>> > String attribute;
>>> > Long count;
>>> > String reference;
>>> > Map<String, Double> properties;
>>> > String granularSynthString = "GranularSynthesis";
>>> >
>>> > while (resultSet.next()) {
>>> > id = resultSet.getString(1);
>>> > spatialType = resultSet.getString(2);
>>> >
>>> > // geometry = wkbReader.read(resultSet.getBytes(3)); // ignored the WKBReader and the results are about the same.
>>> > attribute = resultSet.getString(4);
>>> > count = resultSet.getLong(5);
>>> > reference = resultSet.getString(6);
>>> >
>>> > properties = new HashMap<String, Double>();
>>> > Double aux = resultSet.getDouble(7);
>>> > properties.put("countpersons", aux);
>>> > aux = resultSet.getDouble(8);
>>> > properties.put("countfatals", aux);
>>> > aux = resultSet.getDouble(9);
>>> > properties.put("countdrunks", aux);
>>> > aux = resultSet.getDouble(10);
>>> > properties.put("density", aux);
>>> > }
>>> >
>>> > System.out.println("[QUERY " + Thread.currentThread().getId()
>>> > + "] - done & iterated in "
>>> > + (System.currentTimeMillis() - time) + "ms.");
>>> >
>>> > resultSet.close();
>>> > st.close();
>>> > connection.commit();
>>> > connection.close();
>>> > } catch (SQLException exception) {
>>> > exception.printStackTrace();
>>> > // } catch (ParseException exception) { // ignored from WKBreader
>>> > // exception.printStackTrace();
>>> > }
>>> >
>>> > System.out.println("[End " + Thread.currentThread().getId()
>>> > + "] - done in " + (System.currentTimeMillis() - time) + "ms.");
>>> > }
>>> >
>>> > -------------------- end code --------------------
>>> >
>>> > Although i'm executing this on slow MacbookPro (2.6 core duo, 2Gbs RAM but SSD) and have a 9.1.2 postgres, i've also tested this on a recent retina MBP and the ratio between pgAdmin and JDBC execution is similar.
>>> >
>>> > Is this expected? Can someone point if i'm doing something terrible wrong?
>>> >
>>> > I'm not concerned about the query performance per-se (i know it CAN be optimized), but the differences just using JDBC and iterating the resultSet are really annoying.
>>> >
>>> > Thanks in advance for any help.
>>> >
>>> > Kind regards,
>>> >
>>> > Rui Leal
>>> >
>>> >
>>>
>>>
>>>
>>> --
>>> Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
>>> To make changes to your subscription:
>>> http://www.postgresql.org/mailpref/pgsql-jdbc
>>>
>>
>>
>
^ permalink raw reply [nested|flat] 29+ messages in thread
* Re: JDBC slow performance on resultSet iteration?
@ 2013-11-05 18:03 Dave Cramer <pg@fastcrypt.com>
parent: Rui Pedro Leal <rui.pedro.leal@gmail.com>
0 siblings, 2 replies; 29+ messages in thread
From: Dave Cramer @ 2013-11-05 18:03 UTC (permalink / raw)
To: Rui Pedro Leal <rui.pedro.leal@gmail.com>; +Cc: pgsql-jdbc
I do not know, the best way to figure this out is to use explain analyze in
pg_admin.
What I am debating is the java code is using a cursor. pgAdmin is not.
Try it without setFetchSize()
Dave Cramer
dave.cramer(at)credativ(dot)ca
http://www.credativ.ca
On Tue, Nov 5, 2013 at 1:00 PM, Rui Pedro Leal <rui.pedro.leal@gmail.com>wrote:
> Dave,
>
> I understand what you are saying, but i must get the results, right? :)
>
> I think in pgAdmin the total query time, presented by the query execution
> windows, is also considering the rendering / presenting of the query
> results, so this is comparable ... right?
>
> [] Rui Pedro Leal
> [] rui.pedro.leal@gmail.com
>
> On 2013/11/05, at 17:34, Dave Cramer wrote:
>
> Rui Leal,
>
> You shouldn't see that much of a penatly, but I question how you are
> measuring it. Since you are timing the loop which is using cursors.
>
> Just run a simple query and time it without iterating through the result
> set. That would be equivalent
>
> Dave Cramer
>
> dave.cramer(at)credativ(dot)ca
> http://www.credativ.ca
>
>
> On Tue, Nov 5, 2013 at 12:16 PM, Rui Pedro Leal <rui.pedro.leal@gmail.com>wrote:
>
>> Hello Dave, thanks for the quick reply.
>>
>> I'm just executing the same query on pgAdmin query interface. Just a way
>> to measure performance of the query + getting results vs the JDBC way.
>>
>> I'm using fetchsize in order to get some faster results when iterating
>> the resultSet. I've tried not to use fetchSize/cursor but the performance
>> is naturally slower (and worser than the simple pgAdmin query).
>>
>> What i'm asking is .. is this performance penalty i'm getting using
>> postgres JDBC normal and within what's expected? I'm i doing something
>> wrong? Can i do it in any other way?
>>
>> Regards,
>>
>> Rui Leal
>>
>> [] Rui Pedro Leal
>> [] rui.pedro.leal@gmail.com
>>
>>
>> On 2013/11/05, at 16:43, Dave Cramer wrote:
>>
>> How can you measure the same query in pgAdmin ? JDBC will use a cursor
>> when you set the fetchsize.
>>
>> Dave Cramer
>>
>> dave.cramer(at)credativ(dot)ca
>> http://www.credativ.ca
>>
>>
>> On Tue, Nov 5, 2013 at 11:12 AM, Rui Pedro Leal <rui.pedro.leal@gmail.com
>> > wrote:
>>
>>> Just an quick info update: i'm using the postgresql-9.2-1003-jdbc4 driver
>>>
>>> On 2013/11/05, at 16:09, Rui Pedro Leal wrote:
>>>
>>> > Hello everyone,
>>> >
>>> > I'm having slow JDBC performance on iterating a resultSet obtained
>>> from a somewhat simple query.
>>> >
>>> > The query, although using PostGIS functions, is pretty straight
>>> forward:
>>> >
>>> > SELECT _id, _spatial_type, ST_AsBinary(_geometry), _attribute, _count,
>>> _references, countpersons, countfatals, countdrunks, density
>>> > FROM accidents_5
>>> > WHERE (_geometry &&
>>> ST_MakeEnvelope(-126.60644531250001,26.43122806450644,-63.369140625,52.96187505907603,
>>> 4326) )
>>> > ORDER BY _pk_id
>>> >
>>> > The results are the following:
>>> > - Executing the query directly from pgAdmin: ~2807ms
>>> > - Executing from JVM + JDBC: 4184ms
>>> >
>>> > The code i'm executing is pretty much standard:
>>> >
>>> > -------------------- code --------------------
>>> >
>>> > public static void main(final String[] args) throws Exception {
>>> >
>>> > String sql = "SELECT _id, _spatial_type,
>>> ST_AsBinary(_geometry), _attribute, _count, _references, countpersons,
>>> countfatals, countdrunks, density";
>>> > sql += " FROM accidents_5";
>>> > sql += " WHERE (_geometry &&
>>> ST_MakeEnvelope(-126.60644531250001,26.43122806450644,-63.369140625,52.96187505907603,
>>> 4326) )";
>>> > sql += " ORDER BY _pk_id";
>>> >
>>> > System.out.println(sql);
>>> >
>>> > Long time = System.currentTimeMillis();
>>> > try {
>>> > Connection connection =
>>> DataStoreInfo.getDataStores().get(0);
>>> > connection.setAutoCommit(false);
>>> >
>>> > System.out.println("[QUERY " +
>>> Thread.currentThread().getId()
>>> > + "] - connection in "
>>> > + (System.currentTimeMillis() -
>>> time) + "ms.");
>>> >
>>> > Statement st = connection.createStatement(
>>> > ResultSet.TYPE_FORWARD_ONLY,
>>> ResultSet.CONCUR_READ_ONLY,
>>> >
>>> ResultSet.CLOSE_CURSORS_AT_COMMIT);
>>> > st.setFetchSize(250);
>>> >
>>> > System.out.println("[QUERY " +
>>> Thread.currentThread().getId()
>>> > + "] - statement in " +
>>> (System.currentTimeMillis() - time)
>>> > + "ms.");
>>> >
>>> > ResultSet resultSet = st.executeQuery(sql);
>>> >
>>> > System.out.println("[QUERY " +
>>> Thread.currentThread().getId()
>>> > + "] - done in " +
>>> (System.currentTimeMillis() - time)
>>> > + "ms.");
>>> >
>>> > GeometryFactory geofact = new
>>> GeometryFactory(new PrecisionModel(),4326);
>>> > WKBReader wkbReader = new WKBReader(geofact);
>>> >
>>> > String id;
>>> > String spatialType;
>>> > Geometry geometry;
>>> > String attribute;
>>> > Long count;
>>> > String reference;
>>> > Map<String, Double> properties;
>>> > String granularSynthString = "GranularSynthesis";
>>> >
>>> > while (resultSet.next()) {
>>> > id = resultSet.getString(1);
>>> > spatialType = resultSet.getString(2);
>>> >
>>> > // geometry =
>>> wkbReader.read(resultSet.getBytes(3)); // ignored the WKBReader and the
>>> results are about the same.
>>> > attribute = resultSet.getString(4);
>>> > count = resultSet.getLong(5);
>>> > reference = resultSet.getString(6);
>>> >
>>> > properties = new HashMap<String,
>>> Double>();
>>> > Double aux = resultSet.getDouble(7);
>>> > properties.put("countpersons", aux);
>>> > aux = resultSet.getDouble(8);
>>> > properties.put("countfatals", aux);
>>> > aux = resultSet.getDouble(9);
>>> > properties.put("countdrunks", aux);
>>> > aux = resultSet.getDouble(10);
>>> > properties.put("density", aux);
>>> > }
>>> >
>>> > System.out.println("[QUERY " +
>>> Thread.currentThread().getId()
>>> > + "] - done & iterated in "
>>> > + (System.currentTimeMillis() -
>>> time) + "ms.");
>>> >
>>> > resultSet.close();
>>> > st.close();
>>> > connection.commit();
>>> > connection.close();
>>> > } catch (SQLException exception) {
>>> > exception.printStackTrace();
>>> > // } catch (ParseException exception) { // ignored from
>>> WKBreader
>>> > // exception.printStackTrace();
>>> > }
>>> >
>>> > System.out.println("[End " +
>>> Thread.currentThread().getId()
>>> > + "] - done in " +
>>> (System.currentTimeMillis() - time) + "ms.");
>>> > }
>>> >
>>> > -------------------- end code --------------------
>>> >
>>> > Although i'm executing this on slow MacbookPro (2.6 core duo, 2Gbs RAM
>>> but SSD) and have a 9.1.2 postgres, i've also tested this on a recent
>>> retina MBP and the ratio between pgAdmin and JDBC execution is similar.
>>> >
>>> > Is this expected? Can someone point if i'm doing something terrible
>>> wrong?
>>> >
>>> > I'm not concerned about the query performance per-se (i know it CAN be
>>> optimized), but the differences just using JDBC and iterating the resultSet
>>> are really annoying.
>>> >
>>> > Thanks in advance for any help.
>>> >
>>> > Kind regards,
>>> >
>>> > Rui Leal
>>> >
>>> >
>>>
>>>
>>>
>>> --
>>> Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
>>> To make changes to your subscription:
>>> http://www.postgresql.org/mailpref/pgsql-jdbc
>>>
>>
>>
>>
>
>
^ permalink raw reply [nested|flat] 29+ messages in thread
* Re: JDBC slow performance on resultSet iteration?
@ 2013-11-05 18:05 Rui Pedro Leal <rui.pedro.leal@gmail.com>
parent: Edson Richter <edsonrichter@hotmail.com>
0 siblings, 2 replies; 29+ messages in thread
From: Rui Pedro Leal @ 2013-11-05 18:05 UTC (permalink / raw)
To: Edson Richter <edsonrichter@hotmail.com>; +Cc: pgsql-jdbc
Hello Edson.
I'm running everything locally on a laptop. I simply connect to PG via JDBC localhost. Just a simple development setup.
[] Rui Pedro Leal
[] rui.pedro.leal@gmail.com
On 2013/11/05, at 17:47, Edson Richter wrote:
> Are you using a network between your station and the server?
> You're connection using IP address or server name?
> If the client machine has to access server several times (like in a cursor scenario), would be necessary to solve machine names, which could slow down the whole process.
>
> Regards,
>
> Edson
> Em 05/11/2013 15:42, Dave Cramer escreveu:
>> Kevin,
>>
>> Ya, however I am still questioning the basic time measurement.
>>
>> Dave Cramer
>>
>> dave.cramer(at)credativ(dot)ca
>> http://www.credativ.ca
>>
>>
>> On Tue, Nov 5, 2013 at 12:39 PM, Kevin Wooten <kdubb@me.com> wrote:
>> Dave,
>> That is not necessarily true. Most of the drivers data conversion is done in the getXXX calls of the ResultSet. All of which would count as “penalty”. I’m not saying it’s a large penalty, just that this is time that must be considered since pgAdmin is converting and displaying the data as well.
>>
>> On Nov 5, 2013, at 10:34 AM, Dave Cramer <pg@fastcrypt.com> wrote:
>>
>>> Rui Leal,
>>>
>>> You shouldn't see that much of a penatly, but I question how you are measuring it. Since you are timing the loop which is using cursors.
>>>
>>> Just run a simple query and time it without iterating through the result set. That would be equivalent
>>>
>>> Dave Cramer
>>>
>>> dave.cramer(at)credativ(dot)ca
>>> http://www.credativ.ca
>>>
>>>
>>> On Tue, Nov 5, 2013 at 12:16 PM, Rui Pedro Leal <rui.pedro.leal@gmail.com> wrote:
>>> Hello Dave, thanks for the quick reply.
>>>
>>> I'm just executing the same query on pgAdmin query interface. Just a way to measure performance of the query + getting results vs the JDBC way.
>>>
>>> I'm using fetchsize in order to get some faster results when iterating the resultSet. I've tried not to use fetchSize/cursor but the performance is naturally slower (and worser than the simple pgAdmin query).
>>>
>>> What i'm asking is .. is this performance penalty i'm getting using postgres JDBC normal and within what's expected? I'm i doing something wrong? Can i do it in any other way?
>>>
>>> Regards,
>>>
>>> Rui Leal
>>>
>>> [] Rui Pedro Leal
>>> [] rui.pedro.leal@gmail.com
>>>
>>>
>>> On 2013/11/05, at 16:43, Dave Cramer wrote:
>>>
>>>> How can you measure the same query in pgAdmin ? JDBC will use a cursor when you set the fetchsize.
>>>>
>>>> Dave Cramer
>>>>
>>>> dave.cramer(at)credativ(dot)ca
>>>> http://www.credativ.ca
>>>>
>>>>
>>>> On Tue, Nov 5, 2013 at 11:12 AM, Rui Pedro Leal <rui.pedro.leal@gmail.com> wrote:
>>>> Just an quick info update: i'm using the postgresql-9.2-1003-jdbc4 driver
>>>>
>>>> On 2013/11/05, at 16:09, Rui Pedro Leal wrote:
>>>>
>>>> > Hello everyone,
>>>> >
>>>> > I'm having slow JDBC performance on iterating a resultSet obtained from a somewhat simple query.
>>>> >
>>>> > The query, although using PostGIS functions, is pretty straight forward:
>>>> >
>>>> > SELECT _id, _spatial_type, ST_AsBinary(_geometry), _attribute, _count, _references, countpersons, countfatals, countdrunks, density
>>>> > FROM accidents_5
>>>> > WHERE (_geometry && ST_MakeEnvelope(-126.60644531250001,26.43122806450644,-63.369140625,52.96187505907603, 4326) )
>>>> > ORDER BY _pk_id
>>>> >
>>>> > The results are the following:
>>>> > - Executing the query directly from pgAdmin: ~2807ms
>>>> > - Executing from JVM + JDBC: 4184ms
>>>> >
>>>> > The code i'm executing is pretty much standard:
>>>> >
>>>> > -------------------- code --------------------
>>>> >
>>>> > public static void main(final String[] args) throws Exception {
>>>> >
>>>> > String sql = "SELECT _id, _spatial_type, ST_AsBinary(_geometry), _attribute, _count, _references, countpersons, countfatals, countdrunks, density";
>>>> > sql += " FROM accidents_5";
>>>> > sql += " WHERE (_geometry && ST_MakeEnvelope(-126.60644531250001,26.43122806450644,-63.369140625,52.96187505907603, 4326) )";
>>>> > sql += " ORDER BY _pk_id";
>>>> >
>>>> > System.out.println(sql);
>>>> >
>>>> > Long time = System.currentTimeMillis();
>>>> > try {
>>>> > Connection connection = DataStoreInfo.getDataStores().get(0);
>>>> > connection.setAutoCommit(false);
>>>> >
>>>> > System.out.println("[QUERY " + Thread.currentThread().getId()
>>>> > + "] - connection in "
>>>> > + (System.currentTimeMillis() - time) + "ms.");
>>>> >
>>>> > Statement st = connection.createStatement(
>>>> > ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY,
>>>> > ResultSet.CLOSE_CURSORS_AT_COMMIT);
>>>> > st.setFetchSize(250);
>>>> >
>>>> > System.out.println("[QUERY " + Thread.currentThread().getId()
>>>> > + "] - statement in " + (System.currentTimeMillis() - time)
>>>> > + "ms.");
>>>> >
>>>> > ResultSet resultSet = st.executeQuery(sql);
>>>> >
>>>> > System.out.println("[QUERY " + Thread.currentThread().getId()
>>>> > + "] - done in " + (System.currentTimeMillis() - time)
>>>> > + "ms.");
>>>> >
>>>> > GeometryFactory geofact = new GeometryFactory(new PrecisionModel(),4326);
>>>> > WKBReader wkbReader = new WKBReader(geofact);
>>>> >
>>>> > String id;
>>>> > String spatialType;
>>>> > Geometry geometry;
>>>> > String attribute;
>>>> > Long count;
>>>> > String reference;
>>>> > Map<String, Double> properties;
>>>> > String granularSynthString = "GranularSynthesis";
>>>> >
>>>> > while (resultSet.next()) {
>>>> > id = resultSet.getString(1);
>>>> > spatialType = resultSet.getString(2);
>>>> >
>>>> > // geometry = wkbReader.read(resultSet.getBytes(3)); // ignored the WKBReader and the results are about the same.
>>>> > attribute = resultSet.getString(4);
>>>> > count = resultSet.getLong(5);
>>>> > reference = resultSet.getString(6);
>>>> >
>>>> > properties = new HashMap<String, Double>();
>>>> > Double aux = resultSet.getDouble(7);
>>>> > properties.put("countpersons", aux);
>>>> > aux = resultSet.getDouble(8);
>>>> > properties.put("countfatals", aux);
>>>> > aux = resultSet.getDouble(9);
>>>> > properties.put("countdrunks", aux);
>>>> > aux = resultSet.getDouble(10);
>>>> > properties.put("density", aux);
>>>> > }
>>>> >
>>>> > System.out.println("[QUERY " + Thread.currentThread().getId()
>>>> > + "] - done & iterated in "
>>>> > + (System.currentTimeMillis() - time) + "ms.");
>>>> >
>>>> > resultSet.close();
>>>> > st.close();
>>>> > connection.commit();
>>>> > connection.close();
>>>> > } catch (SQLException exception) {
>>>> > exception.printStackTrace();
>>>> > // } catch (ParseException exception) { // ignored from WKBreader
>>>> > // exception.printStackTrace();
>>>> > }
>>>> >
>>>> > System.out.println("[End " + Thread.currentThread().getId()
>>>> > + "] - done in " + (System.currentTimeMillis() - time) + "ms.");
>>>> > }
>>>> >
>>>> > -------------------- end code --------------------
>>>> >
>>>> > Although i'm executing this on slow MacbookPro (2.6 core duo, 2Gbs RAM but SSD) and have a 9.1.2 postgres, i've also tested this on a recent retina MBP and the ratio between pgAdmin and JDBC execution is similar.
>>>> >
>>>> > Is this expected? Can someone point if i'm doing something terrible wrong?
>>>> >
>>>> > I'm not concerned about the query performance per-se (i know it CAN be optimized), but the differences just using JDBC and iterating the resultSet are really annoying.
>>>> >
>>>> > Thanks in advance for any help.
>>>> >
>>>> > Kind regards,
>>>> >
>>>> > Rui Leal
>>>> >
>>>> >
>>>>
>>>>
>>>>
>>>> --
>>>> Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
>>>> To make changes to your subscription:
>>>> http://www.postgresql.org/mailpref/pgsql-jdbc
>>>>
>>>
>>>
>>
^ permalink raw reply [nested|flat] 29+ messages in thread
* Re: JDBC slow performance on resultSet iteration?
@ 2013-11-05 18:08 Dave Cramer <pg@fastcrypt.com>
parent: Rui Pedro Leal <rui.pedro.leal@gmail.com>
1 sibling, 1 reply; 29+ messages in thread
From: Dave Cramer @ 2013-11-05 18:08 UTC (permalink / raw)
To: Rui Pedro Leal <rui.pedro.leal@gmail.com>; +Cc: Edson Richter <edsonrichter@hotmail.com>; pgsql-jdbc
Rui,
The best way to settle this is to turn on log_duration in the server and
get the server timings. That will give you the query time without rendering
Dave Cramer
dave.cramer(at)credativ(dot)ca
http://www.credativ.ca
On Tue, Nov 5, 2013 at 1:05 PM, Rui Pedro Leal <rui.pedro.leal@gmail.com>wrote:
> Hello Edson.
>
> I'm running everything locally on a laptop. I simply connect to PG via
> JDBC localhost. Just a simple development setup.
>
> [] Rui Pedro Leal
> [] rui.pedro.leal@gmail.com
>
> On 2013/11/05, at 17:47, Edson Richter wrote:
>
> Are you using a network between your station and the server?
> You're connection using IP address or server name?
> If the client machine has to access server several times (like in a cursor
> scenario), would be necessary to solve machine names, which could slow down
> the whole process.
>
> Regards,
>
> Edson
>
>
> Em 05/11/2013 15:42, Dave Cramer escreveu:
>
> Kevin,
>
> Ya, however I am still questioning the basic time measurement.
>
> Dave Cramer
>
> dave.cramer(at)credativ(dot)ca
> http://www.credativ.ca
>
>
> On Tue, Nov 5, 2013 at 12:39 PM, Kevin Wooten <kdubb@me.com> wrote:
>
>> Dave,
>> That is not necessarily true. Most of the drivers data conversion is done
>> in the getXXX calls of the ResultSet. All of which would count as
>> “penalty”. I’m not saying it’s a large penalty, just that this is time
>> that must be considered since pgAdmin is converting and displaying the data
>> as well.
>>
>> On Nov 5, 2013, at 10:34 AM, Dave Cramer <pg@fastcrypt.com> wrote:
>>
>> Rui Leal,
>>
>> You shouldn't see that much of a penatly, but I question how you are
>> measuring it. Since you are timing the loop which is using cursors.
>>
>> Just run a simple query and time it without iterating through the
>> result set. That would be equivalent
>>
>> Dave Cramer
>>
>> dave.cramer(at)credativ(dot)ca
>> http://www.credativ.ca
>>
>>
>> On Tue, Nov 5, 2013 at 12:16 PM, Rui Pedro Leal <rui.pedro.leal@gmail.com
>> > wrote:
>>
>>> Hello Dave, thanks for the quick reply.
>>>
>>> I'm just executing the same query on pgAdmin query interface. Just a
>>> way to measure performance of the query + getting results vs the JDBC way.
>>>
>>> I'm using fetchsize in order to get some faster results when iterating
>>> the resultSet. I've tried not to use fetchSize/cursor but the performance
>>> is naturally slower (and worser than the simple pgAdmin query).
>>>
>>> What i'm asking is .. is this performance penalty i'm getting using
>>> postgres JDBC normal and within what's expected? I'm i doing something
>>> wrong? Can i do it in any other way?
>>>
>>> Regards,
>>>
>>> Rui Leal
>>>
>>> [] Rui Pedro Leal
>>> [] rui.pedro.leal@gmail.com
>>>
>>>
>>> On 2013/11/05, at 16:43, Dave Cramer wrote:
>>>
>>> How can you measure the same query in pgAdmin ? JDBC will use a cursor
>>> when you set the fetchsize.
>>>
>>> Dave Cramer
>>>
>>> dave.cramer(at)credativ(dot)ca
>>> http://www.credativ.ca
>>>
>>>
>>> On Tue, Nov 5, 2013 at 11:12 AM, Rui Pedro Leal <
>>> rui.pedro.leal@gmail.com> wrote:
>>>
>>>> Just an quick info update: i'm using the postgresql-9.2-1003-jdbc4
>>>> driver
>>>>
>>>> On 2013/11/05, at 16:09, Rui Pedro Leal wrote:
>>>>
>>>> > Hello everyone,
>>>> >
>>>> > I'm having slow JDBC performance on iterating a resultSet obtained
>>>> from a somewhat simple query.
>>>> >
>>>> > The query, although using PostGIS functions, is pretty straight
>>>> forward:
>>>> >
>>>> > SELECT _id, _spatial_type, ST_AsBinary(_geometry), _attribute,
>>>> _count, _references, countpersons, countfatals, countdrunks, density
>>>> > FROM accidents_5
>>>> > WHERE (_geometry &&
>>>> ST_MakeEnvelope(-126.60644531250001,26.43122806450644,-63.369140625,52.96187505907603,
>>>> 4326) )
>>>> > ORDER BY _pk_id
>>>> >
>>>> > The results are the following:
>>>> > - Executing the query directly from pgAdmin: ~2807ms
>>>> > - Executing from JVM + JDBC: 4184ms
>>>> >
>>>> > The code i'm executing is pretty much standard:
>>>> >
>>>> > -------------------- code --------------------
>>>> >
>>>> > public static void main(final String[] args) throws Exception {
>>>> >
>>>> > String sql = "SELECT _id, _spatial_type,
>>>> ST_AsBinary(_geometry), _attribute, _count, _references, countpersons,
>>>> countfatals, countdrunks, density";
>>>> > sql += " FROM accidents_5";
>>>> > sql += " WHERE (_geometry &&
>>>> ST_MakeEnvelope(-126.60644531250001,26.43122806450644,-63.369140625,52.96187505907603,
>>>> 4326) )";
>>>> > sql += " ORDER BY _pk_id";
>>>> >
>>>> > System.out.println(sql);
>>>> >
>>>> > Long time = System.currentTimeMillis();
>>>> > try {
>>>> > Connection connection =
>>>> DataStoreInfo.getDataStores().get(0);
>>>> > connection.setAutoCommit(false);
>>>> >
>>>> > System.out.println("[QUERY " +
>>>> Thread.currentThread().getId()
>>>> > + "] - connection in "
>>>> > + (System.currentTimeMillis() -
>>>> time) + "ms.");
>>>> >
>>>> > Statement st = connection.createStatement(
>>>> > ResultSet.TYPE_FORWARD_ONLY,
>>>> ResultSet.CONCUR_READ_ONLY,
>>>> >
>>>> ResultSet.CLOSE_CURSORS_AT_COMMIT);
>>>> > st.setFetchSize(250);
>>>> >
>>>> > System.out.println("[QUERY " +
>>>> Thread.currentThread().getId()
>>>> > + "] - statement in " +
>>>> (System.currentTimeMillis() - time)
>>>> > + "ms.");
>>>> >
>>>> > ResultSet resultSet = st.executeQuery(sql);
>>>> >
>>>> > System.out.println("[QUERY " +
>>>> Thread.currentThread().getId()
>>>> > + "] - done in " +
>>>> (System.currentTimeMillis() - time)
>>>> > + "ms.");
>>>> >
>>>> > GeometryFactory geofact = new
>>>> GeometryFactory(new PrecisionModel(),4326);
>>>> > WKBReader wkbReader = new WKBReader(geofact);
>>>> >
>>>> > String id;
>>>> > String spatialType;
>>>> > Geometry geometry;
>>>> > String attribute;
>>>> > Long count;
>>>> > String reference;
>>>> > Map<String, Double> properties;
>>>> > String granularSynthString =
>>>> "GranularSynthesis";
>>>> >
>>>> > while (resultSet.next()) {
>>>> > id = resultSet.getString(1);
>>>> > spatialType = resultSet.getString(2);
>>>> >
>>>> > // geometry =
>>>> wkbReader.read(resultSet.getBytes(3)); // ignored the WKBReader and the
>>>> results are about the same.
>>>> > attribute = resultSet.getString(4);
>>>> > count = resultSet.getLong(5);
>>>> > reference = resultSet.getString(6);
>>>> >
>>>> > properties = new HashMap<String,
>>>> Double>();
>>>> > Double aux = resultSet.getDouble(7);
>>>> > properties.put("countpersons", aux);
>>>> > aux = resultSet.getDouble(8);
>>>> > properties.put("countfatals", aux);
>>>> > aux = resultSet.getDouble(9);
>>>> > properties.put("countdrunks", aux);
>>>> > aux = resultSet.getDouble(10);
>>>> > properties.put("density", aux);
>>>> > }
>>>> >
>>>> > System.out.println("[QUERY " +
>>>> Thread.currentThread().getId()
>>>> > + "] - done & iterated in "
>>>> > + (System.currentTimeMillis() -
>>>> time) + "ms.");
>>>> >
>>>> > resultSet.close();
>>>> > st.close();
>>>> > connection.commit();
>>>> > connection.close();
>>>> > } catch (SQLException exception) {
>>>> > exception.printStackTrace();
>>>> > // } catch (ParseException exception) { // ignored
>>>> from WKBreader
>>>> > // exception.printStackTrace();
>>>> > }
>>>> >
>>>> > System.out.println("[End " +
>>>> Thread.currentThread().getId()
>>>> > + "] - done in " +
>>>> (System.currentTimeMillis() - time) + "ms.");
>>>> > }
>>>> >
>>>> > -------------------- end code --------------------
>>>> >
>>>> > Although i'm executing this on slow MacbookPro (2.6 core duo, 2Gbs
>>>> RAM but SSD) and have a 9.1.2 postgres, i've also tested this on a recent
>>>> retina MBP and the ratio between pgAdmin and JDBC execution is similar.
>>>> >
>>>> > Is this expected? Can someone point if i'm doing something terrible
>>>> wrong?
>>>> >
>>>> > I'm not concerned about the query performance per-se (i know it CAN
>>>> be optimized), but the differences just using JDBC and iterating the
>>>> resultSet are really annoying.
>>>> >
>>>> > Thanks in advance for any help.
>>>> >
>>>> > Kind regards,
>>>> >
>>>> > Rui Leal
>>>> >
>>>> >
>>>>
>>>>
>>>>
>>>> --
>>>> Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
>>>> To make changes to your subscription:
>>>> http://www.postgresql.org/mailpref/pgsql-jdbc
>>>>
>>>
>>>
>>>
>>
>>
>
^ permalink raw reply [nested|flat] 29+ messages in thread
* Re: JDBC slow performance on resultSet iteration?
@ 2013-11-05 18:11 Rui Pedro Leal <rui.pedro.leal@gmail.com>
parent: Dave Cramer <pg@fastcrypt.com>
1 sibling, 1 reply; 29+ messages in thread
From: Rui Pedro Leal @ 2013-11-05 18:11 UTC (permalink / raw)
To: Dave Cramer <pg@fastcrypt.com>; +Cc: pgsql-jdbc
Dave,
Sure ... here it is ...
I've just replaced this:
--------------- code ---------------
Statement st = connection.createStatement(
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY,
ResultSet.CLOSE_CURSORS_AT_COMMIT);
st.setFetchSize(250);
ResultSet resultSet = st.executeQuery(sql);
------------- end code -------------
With this:
--------------- code ---------------
Statement st = connection.createStatement();
ResultSet resultSet = st.executeQuery(sql);
------------- end code -------------
Result time is ~4760ms (vs. ~4184ms using the above fetchSize).
Thanks for the quick replies.
[] Rui Pedro Leal
[] rui.pedro.leal@gmail.com
On 2013/11/05, at 18:03, Dave Cramer wrote:
> I do not know, the best way to figure this out is to use explain analyze in pg_admin.
>
> What I am debating is the java code is using a cursor. pgAdmin is not.
>
> Try it without setFetchSize()
>
> Dave Cramer
>
> dave.cramer(at)credativ(dot)ca
> http://www.credativ.ca
>
>
> On Tue, Nov 5, 2013 at 1:00 PM, Rui Pedro Leal <rui.pedro.leal@gmail.com> wrote:
> Dave,
>
> I understand what you are saying, but i must get the results, right? :)
>
> I think in pgAdmin the total query time, presented by the query execution windows, is also considering the rendering / presenting of the query results, so this is comparable ... right?
>
> [] Rui Pedro Leal
> [] rui.pedro.leal@gmail.com
>
> On 2013/11/05, at 17:34, Dave Cramer wrote:
>
>> Rui Leal,
>>
>> You shouldn't see that much of a penatly, but I question how you are measuring it. Since you are timing the loop which is using cursors.
>>
>> Just run a simple query and time it without iterating through the result set. That would be equivalent
>>
>> Dave Cramer
>>
>> dave.cramer(at)credativ(dot)ca
>> http://www.credativ.ca
>>
>>
>> On Tue, Nov 5, 2013 at 12:16 PM, Rui Pedro Leal <rui.pedro.leal@gmail.com> wrote:
>> Hello Dave, thanks for the quick reply.
>>
>> I'm just executing the same query on pgAdmin query interface. Just a way to measure performance of the query + getting results vs the JDBC way.
>>
>> I'm using fetchsize in order to get some faster results when iterating the resultSet. I've tried not to use fetchSize/cursor but the performance is naturally slower (and worser than the simple pgAdmin query).
>>
>> What i'm asking is .. is this performance penalty i'm getting using postgres JDBC normal and within what's expected? I'm i doing something wrong? Can i do it in any other way?
>>
>> Regards,
>>
>> Rui Leal
>>
>> [] Rui Pedro Leal
>> [] rui.pedro.leal@gmail.com
>>
>>
>> On 2013/11/05, at 16:43, Dave Cramer wrote:
>>
>>> How can you measure the same query in pgAdmin ? JDBC will use a cursor when you set the fetchsize.
>>>
>>> Dave Cramer
>>>
>>> dave.cramer(at)credativ(dot)ca
>>> http://www.credativ.ca
>>>
>>>
>>> On Tue, Nov 5, 2013 at 11:12 AM, Rui Pedro Leal <rui.pedro.leal@gmail.com> wrote:
>>> Just an quick info update: i'm using the postgresql-9.2-1003-jdbc4 driver
>>>
>>> On 2013/11/05, at 16:09, Rui Pedro Leal wrote:
>>>
>>> > Hello everyone,
>>> >
>>> > I'm having slow JDBC performance on iterating a resultSet obtained from a somewhat simple query.
>>> >
>>> > The query, although using PostGIS functions, is pretty straight forward:
>>> >
>>> > SELECT _id, _spatial_type, ST_AsBinary(_geometry), _attribute, _count, _references, countpersons, countfatals, countdrunks, density
>>> > FROM accidents_5
>>> > WHERE (_geometry && ST_MakeEnvelope(-126.60644531250001,26.43122806450644,-63.369140625,52.96187505907603, 4326) )
>>> > ORDER BY _pk_id
>>> >
>>> > The results are the following:
>>> > - Executing the query directly from pgAdmin: ~2807ms
>>> > - Executing from JVM + JDBC: 4184ms
>>> >
>>> > The code i'm executing is pretty much standard:
>>> >
>>> > -------------------- code --------------------
>>> >
>>> > public static void main(final String[] args) throws Exception {
>>> >
>>> > String sql = "SELECT _id, _spatial_type, ST_AsBinary(_geometry), _attribute, _count, _references, countpersons, countfatals, countdrunks, density";
>>> > sql += " FROM accidents_5";
>>> > sql += " WHERE (_geometry && ST_MakeEnvelope(-126.60644531250001,26.43122806450644,-63.369140625,52.96187505907603, 4326) )";
>>> > sql += " ORDER BY _pk_id";
>>> >
>>> > System.out.println(sql);
>>> >
>>> > Long time = System.currentTimeMillis();
>>> > try {
>>> > Connection connection = DataStoreInfo.getDataStores().get(0);
>>> > connection.setAutoCommit(false);
>>> >
>>> > System.out.println("[QUERY " + Thread.currentThread().getId()
>>> > + "] - connection in "
>>> > + (System.currentTimeMillis() - time) + "ms.");
>>> >
>>> > Statement st = connection.createStatement(
>>> > ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY,
>>> > ResultSet.CLOSE_CURSORS_AT_COMMIT);
>>> > st.setFetchSize(250);
>>> >
>>> > System.out.println("[QUERY " + Thread.currentThread().getId()
>>> > + "] - statement in " + (System.currentTimeMillis() - time)
>>> > + "ms.");
>>> >
>>> > ResultSet resultSet = st.executeQuery(sql);
>>> >
>>> > System.out.println("[QUERY " + Thread.currentThread().getId()
>>> > + "] - done in " + (System.currentTimeMillis() - time)
>>> > + "ms.");
>>> >
>>> > GeometryFactory geofact = new GeometryFactory(new PrecisionModel(),4326);
>>> > WKBReader wkbReader = new WKBReader(geofact);
>>> >
>>> > String id;
>>> > String spatialType;
>>> > Geometry geometry;
>>> > String attribute;
>>> > Long count;
>>> > String reference;
>>> > Map<String, Double> properties;
>>> > String granularSynthString = "GranularSynthesis";
>>> >
>>> > while (resultSet.next()) {
>>> > id = resultSet.getString(1);
>>> > spatialType = resultSet.getString(2);
>>> >
>>> > // geometry = wkbReader.read(resultSet.getBytes(3)); // ignored the WKBReader and the results are about the same.
>>> > attribute = resultSet.getString(4);
>>> > count = resultSet.getLong(5);
>>> > reference = resultSet.getString(6);
>>> >
>>> > properties = new HashMap<String, Double>();
>>> > Double aux = resultSet.getDouble(7);
>>> > properties.put("countpersons", aux);
>>> > aux = resultSet.getDouble(8);
>>> > properties.put("countfatals", aux);
>>> > aux = resultSet.getDouble(9);
>>> > properties.put("countdrunks", aux);
>>> > aux = resultSet.getDouble(10);
>>> > properties.put("density", aux);
>>> > }
>>> >
>>> > System.out.println("[QUERY " + Thread.currentThread().getId()
>>> > + "] - done & iterated in "
>>> > + (System.currentTimeMillis() - time) + "ms.");
>>> >
>>> > resultSet.close();
>>> > st.close();
>>> > connection.commit();
>>> > connection.close();
>>> > } catch (SQLException exception) {
>>> > exception.printStackTrace();
>>> > // } catch (ParseException exception) { // ignored from WKBreader
>>> > // exception.printStackTrace();
>>> > }
>>> >
>>> > System.out.println("[End " + Thread.currentThread().getId()
>>> > + "] - done in " + (System.currentTimeMillis() - time) + "ms.");
>>> > }
>>> >
>>> > -------------------- end code --------------------
>>> >
>>> > Although i'm executing this on slow MacbookPro (2.6 core duo, 2Gbs RAM but SSD) and have a 9.1.2 postgres, i've also tested this on a recent retina MBP and the ratio between pgAdmin and JDBC execution is similar.
>>> >
>>> > Is this expected? Can someone point if i'm doing something terrible wrong?
>>> >
>>> > I'm not concerned about the query performance per-se (i know it CAN be optimized), but the differences just using JDBC and iterating the resultSet are really annoying.
>>> >
>>> > Thanks in advance for any help.
>>> >
>>> > Kind regards,
>>> >
>>> > Rui Leal
>>> >
>>> >
>>>
>>>
>>>
>>> --
>>> Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
>>> To make changes to your subscription:
>>> http://www.postgresql.org/mailpref/pgsql-jdbc
>>>
>>
>>
>
>
^ permalink raw reply [nested|flat] 29+ messages in thread
* Re: JDBC slow performance on resultSet iteration?
@ 2013-11-05 18:14 Edson Richter <edsonrichter@hotmail.com>
parent: Rui Pedro Leal <rui.pedro.leal@gmail.com>
1 sibling, 1 reply; 29+ messages in thread
From: Edson Richter @ 2013-11-05 18:14 UTC (permalink / raw)
To: pgsql-jdbc
Em 05/11/2013 16:05, Rui Pedro Leal escreveu:
> Hello Edson.
>
> I'm running everything locally on a laptop. I simply connect to PG via
> JDBC localhost. Just a simple development setup.
>
> [] Rui Pedro Leal
> [] rui.pedro.leal@gmail.com <mailto:rui.pedro.leal@gmail.com>
>
> On 2013/11/05, at 17:47, Edson Richter wrote:
>
>> Are you using a network between your station and the server?
>> You're connection using IP address or server name?
>> If the client machine has to access server several times (like in a
>> cursor scenario), would be necessary to solve machine names, which
>> could slow down the whole process.
>>
>> Regards,
>>
>> Edson
>
>> Em 05/11/2013 15:42, Dave Cramer escreveu:
>>> Kevin,
>>>
>>> Ya, however I am still questioning the basic time measurement.
>>>
>>> Dave Cramer
>>>
>>> dave.cramer(at)credativ(dot)ca
>>> http://www.credativ.ca <http://www.credativ.ca/;
>>>
>>>
>>> On Tue, Nov 5, 2013 at 12:39 PM, Kevin Wooten <kdubb@me.com
>>> <mailto:kdubb@me.com>> wrote:
>>>
>>> Dave,
>>> That is not necessarily true. Most of the drivers data
>>> conversion is done in the getXXX calls of the ResultSet. All of
>>> which would count as "penalty". I'm not saying it's a large
>>> penalty, just that this is time that must be considered since
>>> pgAdmin is converting and displaying the data as well.
>>>
>>> On Nov 5, 2013, at 10:34 AM, Dave Cramer <pg@fastcrypt.com
>>> <mailto:pg@fastcrypt.com>> wrote:
>>>
>>>> Rui Leal,
>>>>
>>>> You shouldn't see that much of a penatly, but I question how
>>>> you are measuring it. Since you are timing the loop which is
>>>> using cursors.
>>>>
>>>> Just run a simple query and time it without iterating through
>>>> the result set. That would be equivalent
>>>>
>>>> Dave Cramer
>>>>
>>>> dave.cramer(at)credativ(dot)ca
>>>> http://www.credativ.ca <http://www.credativ.ca/;
>>>>
>>>>
>>>> On Tue, Nov 5, 2013 at 12:16 PM, Rui Pedro Leal
>>>> <rui.pedro.leal@gmail.com <mailto:rui.pedro.leal@gmail.com>> wrote:
>>>>
>>>> Hello Dave, thanks for the quick reply.
>>>>
>>>> I'm just executing the same query on pgAdmin query
>>>> interface. Just a way to measure performance of the query +
>>>> getting results vs the JDBC way.
>>>>
>>>> I'm using fetchsize in order to get some faster results
>>>> when iterating the resultSet. I've tried not to use
>>>> fetchSize/cursor but the performance is naturally slower
>>>> (and worser than the simple pgAdmin query).
>>>>
>>>> What i'm asking is .. is this performance penalty i'm
>>>> getting using postgres JDBC normal and within what's
>>>> expected? I'm i doing something wrong? Can i do it in any
>>>> other way?
>>>>
>>>> Regards,
>>>>
>>>> Rui Leal
>>>>
>>>> [] Rui Pedro Leal
>>>> [] rui.pedro.leal@gmail.com <mailto:rui.pedro.leal@gmail.com>
>>>>
>>>>
>>>> On 2013/11/05, at 16:43, Dave Cramer wrote:
>>>>
>>>>> How can you measure the same query in pgAdmin ? JDBC will
>>>>> use a cursor when you set the fetchsize.
>>>>>
>>>>> Dave Cramer
>>>>>
>>>>> dave.cramer(at)credativ(dot)ca
>>>>> http://www.credativ.ca <http://www.credativ.ca/;
>>>>>
>>>>>
>>>>> On Tue, Nov 5, 2013 at 11:12 AM, Rui Pedro Leal
>>>>> <rui.pedro.leal@gmail.com
>>>>> <mailto:rui.pedro.leal@gmail.com>> wrote:
>>>>>
>>>>> Just an quick info update: i'm using the
>>>>> postgresql-9.2-1003-jdbc4 driver
>>>>>
>>>>> On 2013/11/05, at 16:09, Rui Pedro Leal wrote:
>>>>>
>>>>> > Hello everyone,
>>>>> >
>>>>> > I'm having slow JDBC performance on iterating a
>>>>> resultSet obtained from a somewhat simple query.
>>>>> >
>>>>> > The query, although using PostGIS functions, is
>>>>> pretty straight forward:
>>>>> >
>>>>> > SELECT _id, _spatial_type, ST_AsBinary(_geometry),
>>>>> _attribute, _count, _references, countpersons,
>>>>> countfatals, countdrunks, density
>>>>> > FROM accidents_5
>>>>> > WHERE (_geometry &&
>>>>> ST_MakeEnvelope(-126.60644531250001,26.43122806450644,-63.369140625,52.96187505907603,
>>>>> 4326) )
>>>>> > ORDER BY _pk_id
>>>>> >
>>>>> > The results are the following:
>>>>> > - Executing the query directly from pgAdmin: ~2807ms
>>>>> > - Executing from JVM + JDBC: 4184ms
>>>>> >
>>>>> > The code i'm executing is pretty much standard:
>>>>> >
>>>>> > -------------------- code --------------------
>>>>> >
>>>>> > public static void main(final String[] args) throws
>>>>> Exception {
>>>>> >
>>>>> > String sql = "SELECT _id, _spatial_type,
>>>>> ST_AsBinary(_geometry), _attribute, _count,
>>>>> _references, countpersons, countfatals, countdrunks,
>>>>> density";
>>>>> > sql += " FROM accidents_5";
>>>>> > sql += " WHERE (_geometry &&
>>>>> ST_MakeEnvelope(-126.60644531250001,26.43122806450644,-63.369140625,52.96187505907603,
>>>>> 4326) )";
>>>>> > sql += " ORDER BY _pk_id";
>>>>> >
>>>>> > System.out.println(sql);
>>>>> >
>>>>> > Long time = System.currentTimeMillis();
>>>>> > try {
>>>>> > Connection connection =
>>>>> DataStoreInfo.getDataStores().get(0);
>>>>> > connection.setAutoCommit(false);
>>>>> >
>>>>> > System.out.println("[QUERY " +
>>>>> Thread.currentThread().getId()
>>>>> > + "] - connection in "
>>>>> > + (System.currentTimeMillis() - time) + "ms.");
>>>>> >
>>>>> > Statement st = connection.createStatement(
>>>>> > ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY,
>>>>> > ResultSet.CLOSE_CURSORS_AT_COMMIT);
>>>>> > st.setFetchSize(250);
>>>>> >
>>>>> > System.out.println("[QUERY " +
>>>>> Thread.currentThread().getId()
>>>>> > + "] - statement in " + (System.currentTimeMillis()
>>>>> - time)
>>>>> > + "ms.");
>>>>> >
>>>>> > ResultSet resultSet = st.executeQuery(sql);
>>>>> >
>>>>> > System.out.println("[QUERY " +
>>>>> Thread.currentThread().getId()
>>>>> > + "] - done in " + (System.currentTimeMillis() - time)
>>>>> > + "ms.");
>>>>> >
>>>>> > GeometryFactory geofact = new GeometryFactory(new
>>>>> PrecisionModel(),4326);
>>>>> > WKBReader wkbReader = new WKBReader(geofact);
>>>>> >
>>>>> > String id;
>>>>> > String spatialType;
>>>>> > Geometry geometry;
>>>>> > String attribute;
>>>>> > Long count;
>>>>> > String reference;
>>>>> > Map<String, Double> properties;
>>>>> > String granularSynthString = "GranularSynthesis";
>>>>> >
>>>>> > while (resultSet.next()) {
>>>>> > id = resultSet.getString(1);
>>>>> > spatialType = resultSet.getString(2);
>>>>> >
>>>>> > // geometry =
>>>>> wkbReader.read(resultSet.getBytes(3)); // ignored the
>>>>> WKBReader and the results are about the same.
>>>>> > attribute = resultSet.getString(4);
>>>>> > count = resultSet.getLong(5);
>>>>> > reference = resultSet.getString(6);
>>>>> >
>>>>> > properties = new HashMap<String, Double>();
>>>>> > Double aux = resultSet.getDouble(7);
>>>>> > properties.put("countpersons", aux);
>>>>> > aux = resultSet.getDouble(8);
>>>>> > properties.put("countfatals", aux);
>>>>> > aux = resultSet.getDouble(9);
>>>>> > properties.put("countdrunks", aux);
>>>>> > aux = resultSet.getDouble(10);
>>>>> > properties.put("density", aux);
>>>>> > }
>>>>> >
>>>>> > System.out.println("[QUERY " +
>>>>> Thread.currentThread().getId()
>>>>> > + "] - done & iterated in "
>>>>> > + (System.currentTimeMillis() - time) + "ms.");
>>>>> >
>>>>> > resultSet.close();
>>>>> > st.close();
>>>>> > connection.commit();
>>>>> > connection.close();
>>>>> > } catch (SQLException exception) {
>>>>> > exception.printStackTrace();
>>>>> > // } catch (ParseException exception) { //
>>>>> ignored from WKBreader
>>>>> > // exception.printStackTrace();
>>>>> > }
>>>>> >
>>>>> > System.out.println("[End " +
>>>>> Thread.currentThread().getId()
>>>>> > + "] - done in " + (System.currentTimeMillis()
>>>>> - time) + "ms.");
>>>>> > }
>>>>> >
>>>>> > -------------------- end code --------------------
>>>>> >
>>>>> > Although i'm executing this on slow MacbookPro (2.6
>>>>> core duo, 2Gbs RAM but SSD) and have a 9.1.2 postgres,
>>>>> i've also tested this on a recent retina MBP and the
>>>>> ratio between pgAdmin and JDBC execution is similar.
>>>>> >
>>>>> > Is this expected? Can someone point if i'm doing
>>>>> something terrible wrong?
>>>>> >
>>>>> > I'm not concerned about the query performance per-se
>>>>> (i know it CAN be optimized), but the differences just
>>>>> using JDBC and iterating the resultSet are really
>>>>> annoying.
>>>>> >
>>>>> > Thanks in advance for any help.
>>>>> >
>>>>> > Kind regards,
>>>>> >
>>>>> > Rui Leal
>>>>> >
>>>>> >
>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> Sent via pgsql-jdbc mailing list
>>>>> (pgsql-jdbc@postgresql.org
>>>>> <mailto:pgsql-jdbc@postgresql.org>)
>>>>> To make changes to your subscription:
>>>>> http://www.postgresql.org/mailpref/pgsql-jdbc
>>>>>
>>>>>
>>>>
>>>>
>>>
>
Pedro,
Would worth to change the "Long time = System.currentTimeMillis();" to
just before the getResultSet(), otherwise other variables would be
affecting results... like prepare time (and also initial connection).
Edson
^ permalink raw reply [nested|flat] 29+ messages in thread
* Re: JDBC slow performance on resultSet iteration?
@ 2013-11-05 18:14 Kevin Wooten <kdubb@me.com>
parent: Dave Cramer <pg@fastcrypt.com>
1 sibling, 1 reply; 29+ messages in thread
From: Kevin Wooten @ 2013-11-05 18:14 UTC (permalink / raw)
To: Dave Cramer <pg@fastcrypt.com>; +Cc: Rui Pedro Leal <rui.pedro.leal@gmail.com>; pgsql-jdbc
Well I can speak from experience…
When I was comparing the performance of my implementation with pgjdbc I was very disheartened at the beginning as pgjdbc was seemingly much faster. It was only after I switched the test to take into account the getXXX calls that the true cost of using the driver was discovered. If you just execute a query and ignore the results pgjdbc is essentially just receiving a stream of text. It ignores all of the work of turning that text into Java objects/primitives.
My implementation uses binary protocol for this reason and that removes a lot of the overhead that pgjdbc has. If pgAdmin is using the binary protocol as well this could very well be the difference that is being seen, because it is a significant amount of time.
On Nov 5, 2013, at 11:03 AM, Dave Cramer <pg@fastcrypt.com> wrote:
> I do not know, the best way to figure this out is to use explain analyze in pg_admin.
>
> What I am debating is the java code is using a cursor. pgAdmin is not.
>
> Try it without setFetchSize()
>
> Dave Cramer
>
> dave.cramer(at)credativ(dot)ca
> http://www.credativ.ca
>
>
> On Tue, Nov 5, 2013 at 1:00 PM, Rui Pedro Leal <rui.pedro.leal@gmail.com> wrote:
> Dave,
>
> I understand what you are saying, but i must get the results, right? :)
>
> I think in pgAdmin the total query time, presented by the query execution windows, is also considering the rendering / presenting of the query results, so this is comparable ... right?
>
> [] Rui Pedro Leal
> [] rui.pedro.leal@gmail.com
>
> On 2013/11/05, at 17:34, Dave Cramer wrote:
>
>> Rui Leal,
>>
>> You shouldn't see that much of a penatly, but I question how you are measuring it. Since you are timing the loop which is using cursors.
>>
>> Just run a simple query and time it without iterating through the result set. That would be equivalent
>>
>> Dave Cramer
>>
>> dave.cramer(at)credativ(dot)ca
>> http://www.credativ.ca
>>
>>
>> On Tue, Nov 5, 2013 at 12:16 PM, Rui Pedro Leal <rui.pedro.leal@gmail.com> wrote:
>> Hello Dave, thanks for the quick reply.
>>
>> I'm just executing the same query on pgAdmin query interface. Just a way to measure performance of the query + getting results vs the JDBC way.
>>
>> I'm using fetchsize in order to get some faster results when iterating the resultSet. I've tried not to use fetchSize/cursor but the performance is naturally slower (and worser than the simple pgAdmin query).
>>
>> What i'm asking is .. is this performance penalty i'm getting using postgres JDBC normal and within what's expected? I'm i doing something wrong? Can i do it in any other way?
>>
>> Regards,
>>
>> Rui Leal
>>
>> [] Rui Pedro Leal
>> [] rui.pedro.leal@gmail.com
>>
>>
>> On 2013/11/05, at 16:43, Dave Cramer wrote:
>>
>>> How can you measure the same query in pgAdmin ? JDBC will use a cursor when you set the fetchsize.
>>>
>>> Dave Cramer
>>>
>>> dave.cramer(at)credativ(dot)ca
>>> http://www.credativ.ca
>>>
>>>
>>> On Tue, Nov 5, 2013 at 11:12 AM, Rui Pedro Leal <rui.pedro.leal@gmail.com> wrote:
>>> Just an quick info update: i'm using the postgresql-9.2-1003-jdbc4 driver
>>>
>>> On 2013/11/05, at 16:09, Rui Pedro Leal wrote:
>>>
>>> > Hello everyone,
>>> >
>>> > I'm having slow JDBC performance on iterating a resultSet obtained from a somewhat simple query.
>>> >
>>> > The query, although using PostGIS functions, is pretty straight forward:
>>> >
>>> > SELECT _id, _spatial_type, ST_AsBinary(_geometry), _attribute, _count, _references, countpersons, countfatals, countdrunks, density
>>> > FROM accidents_5
>>> > WHERE (_geometry && ST_MakeEnvelope(-126.60644531250001,26.43122806450644,-63.369140625,52.96187505907603, 4326) )
>>> > ORDER BY _pk_id
>>> >
>>> > The results are the following:
>>> > - Executing the query directly from pgAdmin: ~2807ms
>>> > - Executing from JVM + JDBC: 4184ms
>>> >
>>> > The code i'm executing is pretty much standard:
>>> >
>>> > -------------------- code --------------------
>>> >
>>> > public static void main(final String[] args) throws Exception {
>>> >
>>> > String sql = "SELECT _id, _spatial_type, ST_AsBinary(_geometry), _attribute, _count, _references, countpersons, countfatals, countdrunks, density";
>>> > sql += " FROM accidents_5";
>>> > sql += " WHERE (_geometry && ST_MakeEnvelope(-126.60644531250001,26.43122806450644,-63.369140625,52.96187505907603, 4326) )";
>>> > sql += " ORDER BY _pk_id";
>>> >
>>> > System.out.println(sql);
>>> >
>>> > Long time = System.currentTimeMillis();
>>> > try {
>>> > Connection connection = DataStoreInfo.getDataStores().get(0);
>>> > connection.setAutoCommit(false);
>>> >
>>> > System.out.println("[QUERY " + Thread.currentThread().getId()
>>> > + "] - connection in "
>>> > + (System.currentTimeMillis() - time) + "ms.");
>>> >
>>> > Statement st = connection.createStatement(
>>> > ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY,
>>> > ResultSet.CLOSE_CURSORS_AT_COMMIT);
>>> > st.setFetchSize(250);
>>> >
>>> > System.out.println("[QUERY " + Thread.currentThread().getId()
>>> > + "] - statement in " + (System.currentTimeMillis() - time)
>>> > + "ms.");
>>> >
>>> > ResultSet resultSet = st.executeQuery(sql);
>>> >
>>> > System.out.println("[QUERY " + Thread.currentThread().getId()
>>> > + "] - done in " + (System.currentTimeMillis() - time)
>>> > + "ms.");
>>> >
>>> > GeometryFactory geofact = new GeometryFactory(new PrecisionModel(),4326);
>>> > WKBReader wkbReader = new WKBReader(geofact);
>>> >
>>> > String id;
>>> > String spatialType;
>>> > Geometry geometry;
>>> > String attribute;
>>> > Long count;
>>> > String reference;
>>> > Map<String, Double> properties;
>>> > String granularSynthString = "GranularSynthesis";
>>> >
>>> > while (resultSet.next()) {
>>> > id = resultSet.getString(1);
>>> > spatialType = resultSet.getString(2);
>>> >
>>> > // geometry = wkbReader.read(resultSet.getBytes(3)); // ignored the WKBReader and the results are about the same.
>>> > attribute = resultSet.getString(4);
>>> > count = resultSet.getLong(5);
>>> > reference = resultSet.getString(6);
>>> >
>>> > properties = new HashMap<String, Double>();
>>> > Double aux = resultSet.getDouble(7);
>>> > properties.put("countpersons", aux);
>>> > aux = resultSet.getDouble(8);
>>> > properties.put("countfatals", aux);
>>> > aux = resultSet.getDouble(9);
>>> > properties.put("countdrunks", aux);
>>> > aux = resultSet.getDouble(10);
>>> > properties.put("density", aux);
>>> > }
>>> >
>>> > System.out.println("[QUERY " + Thread.currentThread().getId()
>>> > + "] - done & iterated in "
>>> > + (System.currentTimeMillis() - time) + "ms.");
>>> >
>>> > resultSet.close();
>>> > st.close();
>>> > connection.commit();
>>> > connection.close();
>>> > } catch (SQLException exception) {
>>> > exception.printStackTrace();
>>> > // } catch (ParseException exception) { // ignored from WKBreader
>>> > // exception.printStackTrace();
>>> > }
>>> >
>>> > System.out.println("[End " + Thread.currentThread().getId()
>>> > + "] - done in " + (System.currentTimeMillis() - time) + "ms.");
>>> > }
>>> >
>>> > -------------------- end code --------------------
>>> >
>>> > Although i'm executing this on slow MacbookPro (2.6 core duo, 2Gbs RAM but SSD) and have a 9.1.2 postgres, i've also tested this on a recent retina MBP and the ratio between pgAdmin and JDBC execution is similar.
>>> >
>>> > Is this expected? Can someone point if i'm doing something terrible wrong?
>>> >
>>> > I'm not concerned about the query performance per-se (i know it CAN be optimized), but the differences just using JDBC and iterating the resultSet are really annoying.
>>> >
>>> > Thanks in advance for any help.
>>> >
>>> > Kind regards,
>>> >
>>> > Rui Leal
>>> >
>>> >
>>>
>>>
>>>
>>> --
>>> Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
>>> To make changes to your subscription:
>>> http://www.postgresql.org/mailpref/pgsql-jdbc
>>>
>>
>>
>
>
^ permalink raw reply [nested|flat] 29+ messages in thread
* Re: JDBC slow performance on resultSet iteration?
@ 2013-11-05 18:16 Dave Cramer <pg@fastcrypt.com>
parent: Rui Pedro Leal <rui.pedro.leal@gmail.com>
0 siblings, 0 replies; 29+ messages in thread
From: Dave Cramer @ 2013-11-05 18:16 UTC (permalink / raw)
To: Rui Pedro Leal <rui.pedro.leal@gmail.com>; +Cc: pgsql-jdbc
And the server timings ? I suspect Kevin is correct here, it is in the
conversion and display
Dave Cramer
dave.cramer(at)credativ(dot)ca
http://www.credativ.ca
On Tue, Nov 5, 2013 at 1:11 PM, Rui Pedro Leal <rui.pedro.leal@gmail.com>wrote:
> Dave,
>
> Sure ... here it is ...
>
> I've just replaced this:
>
> --------------- code ---------------
> Statement st = connection.createStatement(
> ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY,
> ResultSet.CLOSE_CURSORS_AT_COMMIT);
> st.setFetchSize(250);
> ResultSet resultSet = st.executeQuery(sql);
> ------------- end code -------------
>
> With this:
>
> --------------- code ---------------
> Statement st = connection.createStatement();
> ResultSet resultSet = st.executeQuery(sql);
> ------------- end code -------------
>
> Result time is ~4760ms (vs. ~4184ms using the above fetchSize).
>
> Thanks for the quick replies.
>
> [] Rui Pedro Leal
> [] rui.pedro.leal@gmail.com
>
> On 2013/11/05, at 18:03, Dave Cramer wrote:
>
> I do not know, the best way to figure this out is to use explain analyze
> in pg_admin.
>
> What I am debating is the java code is using a cursor. pgAdmin is not.
>
> Try it without setFetchSize()
>
> Dave Cramer
>
> dave.cramer(at)credativ(dot)ca
> http://www.credativ.ca
>
>
> On Tue, Nov 5, 2013 at 1:00 PM, Rui Pedro Leal <rui.pedro.leal@gmail.com>wrote:
>
>> Dave,
>>
>> I understand what you are saying, but i must get the results, right? :)
>>
>> I think in pgAdmin the total query time, presented by the query execution
>> windows, is also considering the rendering / presenting of the query
>> results, so this is comparable ... right?
>>
>> [] Rui Pedro Leal
>> [] rui.pedro.leal@gmail.com
>>
>> On 2013/11/05, at 17:34, Dave Cramer wrote:
>>
>> Rui Leal,
>>
>> You shouldn't see that much of a penatly, but I question how you are
>> measuring it. Since you are timing the loop which is using cursors.
>>
>> Just run a simple query and time it without iterating through the result
>> set. That would be equivalent
>>
>> Dave Cramer
>>
>> dave.cramer(at)credativ(dot)ca
>> http://www.credativ.ca
>>
>>
>> On Tue, Nov 5, 2013 at 12:16 PM, Rui Pedro Leal <rui.pedro.leal@gmail.com
>> > wrote:
>>
>>> Hello Dave, thanks for the quick reply.
>>>
>>> I'm just executing the same query on pgAdmin query interface. Just a way
>>> to measure performance of the query + getting results vs the JDBC way.
>>>
>>> I'm using fetchsize in order to get some faster results when iterating
>>> the resultSet. I've tried not to use fetchSize/cursor but the performance
>>> is naturally slower (and worser than the simple pgAdmin query).
>>>
>>> What i'm asking is .. is this performance penalty i'm getting using
>>> postgres JDBC normal and within what's expected? I'm i doing something
>>> wrong? Can i do it in any other way?
>>>
>>> Regards,
>>>
>>> Rui Leal
>>>
>>> [] Rui Pedro Leal
>>> [] rui.pedro.leal@gmail.com
>>>
>>>
>>> On 2013/11/05, at 16:43, Dave Cramer wrote:
>>>
>>> How can you measure the same query in pgAdmin ? JDBC will use a cursor
>>> when you set the fetchsize.
>>>
>>> Dave Cramer
>>>
>>> dave.cramer(at)credativ(dot)ca
>>> http://www.credativ.ca
>>>
>>>
>>> On Tue, Nov 5, 2013 at 11:12 AM, Rui Pedro Leal <
>>> rui.pedro.leal@gmail.com> wrote:
>>>
>>>> Just an quick info update: i'm using the postgresql-9.2-1003-jdbc4
>>>> driver
>>>>
>>>> On 2013/11/05, at 16:09, Rui Pedro Leal wrote:
>>>>
>>>> > Hello everyone,
>>>> >
>>>> > I'm having slow JDBC performance on iterating a resultSet obtained
>>>> from a somewhat simple query.
>>>> >
>>>> > The query, although using PostGIS functions, is pretty straight
>>>> forward:
>>>> >
>>>> > SELECT _id, _spatial_type, ST_AsBinary(_geometry), _attribute,
>>>> _count, _references, countpersons, countfatals, countdrunks, density
>>>> > FROM accidents_5
>>>> > WHERE (_geometry &&
>>>> ST_MakeEnvelope(-126.60644531250001,26.43122806450644,-63.369140625,52.96187505907603,
>>>> 4326) )
>>>> > ORDER BY _pk_id
>>>> >
>>>> > The results are the following:
>>>> > - Executing the query directly from pgAdmin: ~2807ms
>>>> > - Executing from JVM + JDBC: 4184ms
>>>> >
>>>> > The code i'm executing is pretty much standard:
>>>> >
>>>> > -------------------- code --------------------
>>>> >
>>>> > public static void main(final String[] args) throws Exception {
>>>> >
>>>> > String sql = "SELECT _id, _spatial_type,
>>>> ST_AsBinary(_geometry), _attribute, _count, _references, countpersons,
>>>> countfatals, countdrunks, density";
>>>> > sql += " FROM accidents_5";
>>>> > sql += " WHERE (_geometry &&
>>>> ST_MakeEnvelope(-126.60644531250001,26.43122806450644,-63.369140625,52.96187505907603,
>>>> 4326) )";
>>>> > sql += " ORDER BY _pk_id";
>>>> >
>>>> > System.out.println(sql);
>>>> >
>>>> > Long time = System.currentTimeMillis();
>>>> > try {
>>>> > Connection connection =
>>>> DataStoreInfo.getDataStores().get(0);
>>>> > connection.setAutoCommit(false);
>>>> >
>>>> > System.out.println("[QUERY " +
>>>> Thread.currentThread().getId()
>>>> > + "] - connection in "
>>>> > + (System.currentTimeMillis() -
>>>> time) + "ms.");
>>>> >
>>>> > Statement st = connection.createStatement(
>>>> > ResultSet.TYPE_FORWARD_ONLY,
>>>> ResultSet.CONCUR_READ_ONLY,
>>>> >
>>>> ResultSet.CLOSE_CURSORS_AT_COMMIT);
>>>> > st.setFetchSize(250);
>>>> >
>>>> > System.out.println("[QUERY " +
>>>> Thread.currentThread().getId()
>>>> > + "] - statement in " +
>>>> (System.currentTimeMillis() - time)
>>>> > + "ms.");
>>>> >
>>>> > ResultSet resultSet = st.executeQuery(sql);
>>>> >
>>>> > System.out.println("[QUERY " +
>>>> Thread.currentThread().getId()
>>>> > + "] - done in " +
>>>> (System.currentTimeMillis() - time)
>>>> > + "ms.");
>>>> >
>>>> > GeometryFactory geofact = new
>>>> GeometryFactory(new PrecisionModel(),4326);
>>>> > WKBReader wkbReader = new WKBReader(geofact);
>>>> >
>>>> > String id;
>>>> > String spatialType;
>>>> > Geometry geometry;
>>>> > String attribute;
>>>> > Long count;
>>>> > String reference;
>>>> > Map<String, Double> properties;
>>>> > String granularSynthString =
>>>> "GranularSynthesis";
>>>> >
>>>> > while (resultSet.next()) {
>>>> > id = resultSet.getString(1);
>>>> > spatialType = resultSet.getString(2);
>>>> >
>>>> > // geometry =
>>>> wkbReader.read(resultSet.getBytes(3)); // ignored the WKBReader and the
>>>> results are about the same.
>>>> > attribute = resultSet.getString(4);
>>>> > count = resultSet.getLong(5);
>>>> > reference = resultSet.getString(6);
>>>> >
>>>> > properties = new HashMap<String,
>>>> Double>();
>>>> > Double aux = resultSet.getDouble(7);
>>>> > properties.put("countpersons", aux);
>>>> > aux = resultSet.getDouble(8);
>>>> > properties.put("countfatals", aux);
>>>> > aux = resultSet.getDouble(9);
>>>> > properties.put("countdrunks", aux);
>>>> > aux = resultSet.getDouble(10);
>>>> > properties.put("density", aux);
>>>> > }
>>>> >
>>>> > System.out.println("[QUERY " +
>>>> Thread.currentThread().getId()
>>>> > + "] - done & iterated in "
>>>> > + (System.currentTimeMillis() -
>>>> time) + "ms.");
>>>> >
>>>> > resultSet.close();
>>>> > st.close();
>>>> > connection.commit();
>>>> > connection.close();
>>>> > } catch (SQLException exception) {
>>>> > exception.printStackTrace();
>>>> > // } catch (ParseException exception) { // ignored
>>>> from WKBreader
>>>> > // exception.printStackTrace();
>>>> > }
>>>> >
>>>> > System.out.println("[End " +
>>>> Thread.currentThread().getId()
>>>> > + "] - done in " +
>>>> (System.currentTimeMillis() - time) + "ms.");
>>>> > }
>>>> >
>>>> > -------------------- end code --------------------
>>>> >
>>>> > Although i'm executing this on slow MacbookPro (2.6 core duo, 2Gbs
>>>> RAM but SSD) and have a 9.1.2 postgres, i've also tested this on a recent
>>>> retina MBP and the ratio between pgAdmin and JDBC execution is similar.
>>>> >
>>>> > Is this expected? Can someone point if i'm doing something terrible
>>>> wrong?
>>>> >
>>>> > I'm not concerned about the query performance per-se (i know it CAN
>>>> be optimized), but the differences just using JDBC and iterating the
>>>> resultSet are really annoying.
>>>> >
>>>> > Thanks in advance for any help.
>>>> >
>>>> > Kind regards,
>>>> >
>>>> > Rui Leal
>>>> >
>>>> >
>>>>
>>>>
>>>>
>>>> --
>>>> Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
>>>> To make changes to your subscription:
>>>> http://www.postgresql.org/mailpref/pgsql-jdbc
>>>>
>>>
>>>
>>>
>>
>>
>
>
^ permalink raw reply [nested|flat] 29+ messages in thread
* Re: JDBC slow performance on resultSet iteration?
@ 2013-11-05 18:23 Kevin Wooten <kdubb@me.com>
parent: Dave Cramer <pg@fastcrypt.com>
0 siblings, 1 reply; 29+ messages in thread
From: Kevin Wooten @ 2013-11-05 18:23 UTC (permalink / raw)
To: Dave Cramer <pg@fastcrypt.com>; +Cc: pgsql-jdbc
Am I remembering incorrectly? I thought the driver only used binary for a few types and only if it was a prepared statement?
On Nov 5, 2013, at 11:18 AM, Dave Cramer <pg@fastcrypt.com> wrote:
> I'd still like to know just how bad the getXXX calls are. Also the driver is using binary protocol, so it shouldn't be that bad
>
> Dave Cramer
>
> dave.cramer(at)credativ(dot)ca
> http://www.credativ.ca
>
>
> On Tue, Nov 5, 2013 at 1:16 PM, Kevin Wooten <kdubb@me.com> wrote:
> Dave,
>
> I don’t believe he is not asking about the query time. He is specifically asking about the cost of using JDBC vs whatever pgAdmin is using (which seems to be libpq).
>
>
> On Nov 5, 2013, at 11:08 AM, Dave Cramer <pg@fastcrypt.com> wrote:
>
>> Rui,
>>
>> The best way to settle this is to turn on log_duration in the server and get the server timings. That will give you the query time without rendering
>>
>> Dave Cramer
>>
>> dave.cramer(at)credativ(dot)ca
>> http://www.credativ.ca
>>
>>
>> On Tue, Nov 5, 2013 at 1:05 PM, Rui Pedro Leal <rui.pedro.leal@gmail.com> wrote:
>> Hello Edson.
>>
>> I'm running everything locally on a laptop. I simply connect to PG via JDBC localhost. Just a simple development setup.
>>
>> [] Rui Pedro Leal
>> [] rui.pedro.leal@gmail.com
>>
>> On 2013/11/05, at 17:47, Edson Richter wrote:
>>
>>> Are you using a network between your station and the server?
>>> You're connection using IP address or server name?
>>> If the client machine has to access server several times (like in a cursor scenario), would be necessary to solve machine names, which could slow down the whole process.
>>>
>>> Regards,
>>>
>>> Edson
>>
>>> Em 05/11/2013 15:42, Dave Cramer escreveu:
>>>> Kevin,
>>>>
>>>> Ya, however I am still questioning the basic time measurement.
>>>>
>>>> Dave Cramer
>>>>
>>>> dave.cramer(at)credativ(dot)ca
>>>> http://www.credativ.ca
>>>>
>>>>
>>>> On Tue, Nov 5, 2013 at 12:39 PM, Kevin Wooten <kdubb@me.com> wrote:
>>>> Dave,
>>>> That is not necessarily true. Most of the drivers data conversion is done in the getXXX calls of the ResultSet. All of which would count as “penalty”. I’m not saying it’s a large penalty, just that this is time that must be considered since pgAdmin is converting and displaying the data as well.
>>>>
>>>> On Nov 5, 2013, at 10:34 AM, Dave Cramer <pg@fastcrypt.com> wrote:
>>>>
>>>>> Rui Leal,
>>>>>
>>>>> You shouldn't see that much of a penatly, but I question how you are measuring it. Since you are timing the loop which is using cursors.
>>>>>
>>>>> Just run a simple query and time it without iterating through the result set. That would be equivalent
>>>>>
>>>>> Dave Cramer
>>>>>
>>>>> dave.cramer(at)credativ(dot)ca
>>>>> http://www.credativ.ca
>>>>>
>>>>>
>>>>> On Tue, Nov 5, 2013 at 12:16 PM, Rui Pedro Leal <rui.pedro.leal@gmail.com> wrote:
>>>>> Hello Dave, thanks for the quick reply.
>>>>>
>>>>> I'm just executing the same query on pgAdmin query interface. Just a way to measure performance of the query + getting results vs the JDBC way.
>>>>>
>>>>> I'm using fetchsize in order to get some faster results when iterating the resultSet. I've tried not to use fetchSize/cursor but the performance is naturally slower (and worser than the simple pgAdmin query).
>>>>>
>>>>> What i'm asking is .. is this performance penalty i'm getting using postgres JDBC normal and within what's expected? I'm i doing something wrong? Can i do it in any other way?
>>>>>
>>>>> Regards,
>>>>>
>>>>> Rui Leal
>>>>>
>>>>> [] Rui Pedro Leal
>>>>> [] rui.pedro.leal@gmail.com
>>>>>
>>>>>
>>>>> On 2013/11/05, at 16:43, Dave Cramer wrote:
>>>>>
>>>>>> How can you measure the same query in pgAdmin ? JDBC will use a cursor when you set the fetchsize.
>>>>>>
>>>>>> Dave Cramer
>>>>>>
>>>>>> dave.cramer(at)credativ(dot)ca
>>>>>> http://www.credativ.ca
>>>>>>
>>>>>>
>>>>>> On Tue, Nov 5, 2013 at 11:12 AM, Rui Pedro Leal <rui.pedro.leal@gmail.com> wrote:
>>>>>> Just an quick info update: i'm using the postgresql-9.2-1003-jdbc4 driver
>>>>>>
>>>>>> On 2013/11/05, at 16:09, Rui Pedro Leal wrote:
>>>>>>
>>>>>> > Hello everyone,
>>>>>> >
>>>>>> > I'm having slow JDBC performance on iterating a resultSet obtained from a somewhat simple query.
>>>>>> >
>>>>>> > The query, although using PostGIS functions, is pretty straight forward:
>>>>>> >
>>>>>> > SELECT _id, _spatial_type, ST_AsBinary(_geometry), _attribute, _count, _references, countpersons, countfatals, countdrunks, density
>>>>>> > FROM accidents_5
>>>>>> > WHERE (_geometry && ST_MakeEnvelope(-126.60644531250001,26.43122806450644,-63.369140625,52.96187505907603, 4326) )
>>>>>> > ORDER BY _pk_id
>>>>>> >
>>>>>> > The results are the following:
>>>>>> > - Executing the query directly from pgAdmin: ~2807ms
>>>>>> > - Executing from JVM + JDBC: 4184ms
>>>>>> >
>>>>>> > The code i'm executing is pretty much standard:
>>>>>> >
>>>>>> > -------------------- code --------------------
>>>>>> >
>>>>>> > public static void main(final String[] args) throws Exception {
>>>>>> >
>>>>>> > String sql = "SELECT _id, _spatial_type, ST_AsBinary(_geometry), _attribute, _count, _references, countpersons, countfatals, countdrunks, density";
>>>>>> > sql += " FROM accidents_5";
>>>>>> > sql += " WHERE (_geometry && ST_MakeEnvelope(-126.60644531250001,26.43122806450644,-63.369140625,52.96187505907603, 4326) )";
>>>>>> > sql += " ORDER BY _pk_id";
>>>>>> >
>>>>>> > System.out.println(sql);
>>>>>> >
>>>>>> > Long time = System.currentTimeMillis();
>>>>>> > try {
>>>>>> > Connection connection = DataStoreInfo.getDataStores().get(0);
>>>>>> > connection.setAutoCommit(false);
>>>>>> >
>>>>>> > System.out.println("[QUERY " + Thread.currentThread().getId()
>>>>>> > + "] - connection in "
>>>>>> > + (System.currentTimeMillis() - time) + "ms.");
>>>>>> >
>>>>>> > Statement st = connection.createStatement(
>>>>>> > ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY,
>>>>>> > ResultSet.CLOSE_CURSORS_AT_COMMIT);
>>>>>> > st.setFetchSize(250);
>>>>>> >
>>>>>> > System.out.println("[QUERY " + Thread.currentThread().getId()
>>>>>> > + "] - statement in " + (System.currentTimeMillis() - time)
>>>>>> > + "ms.");
>>>>>> >
>>>>>> > ResultSet resultSet = st.executeQuery(sql);
>>>>>> >
>>>>>> > System.out.println("[QUERY " + Thread.currentThread().getId()
>>>>>> > + "] - done in " + (System.currentTimeMillis() - time)
>>>>>> > + "ms.");
>>>>>> >
>>>>>> > GeometryFactory geofact = new GeometryFactory(new PrecisionModel(),4326);
>>>>>> > WKBReader wkbReader = new WKBReader(geofact);
>>>>>> >
>>>>>> > String id;
>>>>>> > String spatialType;
>>>>>> > Geometry geometry;
>>>>>> > String attribute;
>>>>>> > Long count;
>>>>>> > String reference;
>>>>>> > Map<String, Double> properties;
>>>>>> > String granularSynthString = "GranularSynthesis";
>>>>>> >
>>>>>> > while (resultSet.next()) {
>>>>>> > id = resultSet.getString(1);
>>>>>> > spatialType = resultSet.getString(2);
>>>>>> >
>>>>>> > // geometry = wkbReader.read(resultSet.getBytes(3)); // ignored the WKBReader and the results are about the same.
>>>>>> > attribute = resultSet.getString(4);
>>>>>> > count = resultSet.getLong(5);
>>>>>> > reference = resultSet.getString(6);
>>>>>> >
>>>>>> > properties = new HashMap<String, Double>();
>>>>>> > Double aux = resultSet.getDouble(7);
>>>>>> > properties.put("countpersons", aux);
>>>>>> > aux = resultSet.getDouble(8);
>>>>>> > properties.put("countfatals", aux);
>>>>>> > aux = resultSet.getDouble(9);
>>>>>> > properties.put("countdrunks", aux);
>>>>>> > aux = resultSet.getDouble(10);
>>>>>> > properties.put("density", aux);
>>>>>> > }
>>>>>> >
>>>>>> > System.out.println("[QUERY " + Thread.currentThread().getId()
>>>>>> > + "] - done & iterated in "
>>>>>> > + (System.currentTimeMillis() - time) + "ms.");
>>>>>> >
>>>>>> > resultSet.close();
>>>>>> > st.close();
>>>>>> > connection.commit();
>>>>>> > connection.close();
>>>>>> > } catch (SQLException exception) {
>>>>>> > exception.printStackTrace();
>>>>>> > // } catch (ParseException exception) { // ignored from WKBreader
>>>>>> > // exception.printStackTrace();
>>>>>> > }
>>>>>> >
>>>>>> > System.out.println("[End " + Thread.currentThread().getId()
>>>>>> > + "] - done in " + (System.currentTimeMillis() - time) + "ms.");
>>>>>> > }
>>>>>> >
>>>>>> > -------------------- end code --------------------
>>>>>> >
>>>>>> > Although i'm executing this on slow MacbookPro (2.6 core duo, 2Gbs RAM but SSD) and have a 9.1.2 postgres, i've also tested this on a recent retina MBP and the ratio between pgAdmin and JDBC execution is similar.
>>>>>> >
>>>>>> > Is this expected? Can someone point if i'm doing something terrible wrong?
>>>>>> >
>>>>>> > I'm not concerned about the query performance per-se (i know it CAN be optimized), but the differences just using JDBC and iterating the resultSet are really annoying.
>>>>>> >
>>>>>> > Thanks in advance for any help.
>>>>>> >
>>>>>> > Kind regards,
>>>>>> >
>>>>>> > Rui Leal
>>>>>> >
>>>>>> >
>>>>>>
>>>>>>
>>>>>>
>>>>>> --
>>>>>> Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
>>>>>> To make changes to your subscription:
>>>>>> http://www.postgresql.org/mailpref/pgsql-jdbc
>>>>>>
>>>>>
>>>>>
>>>>
>>
>>
>
>
^ permalink raw reply [nested|flat] 29+ messages in thread
* Re: JDBC slow performance on resultSet iteration?
@ 2013-11-05 18:25 Dave Cramer <pg@fastcrypt.com>
parent: Kevin Wooten <kdubb@me.com>
0 siblings, 1 reply; 29+ messages in thread
From: Dave Cramer @ 2013-11-05 18:25 UTC (permalink / raw)
To: Kevin Wooten <kdubb@me.com>; +Cc: pgsql-jdbc
essentially everything is a prepared statement, and AFAIK, it is using
binary protocol
Dave Cramer
dave.cramer(at)credativ(dot)ca
http://www.credativ.ca
On Tue, Nov 5, 2013 at 1:23 PM, Kevin Wooten <kdubb@me.com> wrote:
> Am I remembering incorrectly? I thought the driver only used binary for
> a few types and only if it was a prepared statement?
>
> On Nov 5, 2013, at 11:18 AM, Dave Cramer <pg@fastcrypt.com> wrote:
>
> I'd still like to know just how bad the getXXX calls are. Also the driver
> is using binary protocol, so it shouldn't be that bad
>
> Dave Cramer
>
> dave.cramer(at)credativ(dot)ca
> http://www.credativ.ca
>
>
> On Tue, Nov 5, 2013 at 1:16 PM, Kevin Wooten <kdubb@me.com> wrote:
>
>> Dave,
>>
>> I don’t believe he is not asking about the query time. He is
>> specifically asking about the cost of using JDBC vs whatever pgAdmin is
>> using (which seems to be libpq).
>>
>>
>> On Nov 5, 2013, at 11:08 AM, Dave Cramer <pg@fastcrypt.com> wrote:
>>
>> Rui,
>>
>> The best way to settle this is to turn on log_duration in the server and
>> get the server timings. That will give you the query time without rendering
>>
>> Dave Cramer
>>
>> dave.cramer(at)credativ(dot)ca
>> http://www.credativ.ca
>>
>>
>> On Tue, Nov 5, 2013 at 1:05 PM, Rui Pedro Leal <rui.pedro.leal@gmail.com>wrote:
>>
>>> Hello Edson.
>>>
>>> I'm running everything locally on a laptop. I simply connect to PG via
>>> JDBC localhost. Just a simple development setup.
>>>
>>> [] Rui Pedro Leal
>>> [] rui.pedro.leal@gmail.com
>>>
>>> On 2013/11/05, at 17:47, Edson Richter wrote:
>>>
>>> Are you using a network between your station and the server?
>>> You're connection using IP address or server name?
>>> If the client machine has to access server several times (like in a
>>> cursor scenario), would be necessary to solve machine names, which could
>>> slow down the whole process.
>>>
>>> Regards,
>>>
>>> Edson
>>>
>>>
>>> Em 05/11/2013 15:42, Dave Cramer escreveu:
>>>
>>> Kevin,
>>>
>>> Ya, however I am still questioning the basic time measurement.
>>>
>>> Dave Cramer
>>>
>>> dave.cramer(at)credativ(dot)ca
>>> http://www.credativ.ca
>>>
>>>
>>> On Tue, Nov 5, 2013 at 12:39 PM, Kevin Wooten <kdubb@me.com> wrote:
>>>
>>>> Dave,
>>>> That is not necessarily true. Most of the drivers data conversion is
>>>> done in the getXXX calls of the ResultSet. All of which would count as
>>>> “penalty”. I’m not saying it’s a large penalty, just that this is time
>>>> that must be considered since pgAdmin is converting and displaying the data
>>>> as well.
>>>>
>>>> On Nov 5, 2013, at 10:34 AM, Dave Cramer <pg@fastcrypt.com> wrote:
>>>>
>>>> Rui Leal,
>>>>
>>>> You shouldn't see that much of a penatly, but I question how you are
>>>> measuring it. Since you are timing the loop which is using cursors.
>>>>
>>>> Just run a simple query and time it without iterating through the
>>>> result set. That would be equivalent
>>>>
>>>> Dave Cramer
>>>>
>>>> dave.cramer(at)credativ(dot)ca
>>>> http://www.credativ.ca
>>>>
>>>>
>>>> On Tue, Nov 5, 2013 at 12:16 PM, Rui Pedro Leal <
>>>> rui.pedro.leal@gmail.com> wrote:
>>>>
>>>>> Hello Dave, thanks for the quick reply.
>>>>>
>>>>> I'm just executing the same query on pgAdmin query interface. Just a
>>>>> way to measure performance of the query + getting results vs the JDBC way.
>>>>>
>>>>> I'm using fetchsize in order to get some faster results when
>>>>> iterating the resultSet. I've tried not to use fetchSize/cursor but the
>>>>> performance is naturally slower (and worser than the simple pgAdmin query).
>>>>>
>>>>> What i'm asking is .. is this performance penalty i'm getting using
>>>>> postgres JDBC normal and within what's expected? I'm i doing something
>>>>> wrong? Can i do it in any other way?
>>>>>
>>>>> Regards,
>>>>>
>>>>> Rui Leal
>>>>>
>>>>> [] Rui Pedro Leal
>>>>> [] rui.pedro.leal@gmail.com
>>>>>
>>>>>
>>>>> On 2013/11/05, at 16:43, Dave Cramer wrote:
>>>>>
>>>>> How can you measure the same query in pgAdmin ? JDBC will use a
>>>>> cursor when you set the fetchsize.
>>>>>
>>>>> Dave Cramer
>>>>>
>>>>> dave.cramer(at)credativ(dot)ca
>>>>> http://www.credativ.ca
>>>>>
>>>>>
>>>>> On Tue, Nov 5, 2013 at 11:12 AM, Rui Pedro Leal <
>>>>> rui.pedro.leal@gmail.com> wrote:
>>>>>
>>>>>> Just an quick info update: i'm using the postgresql-9.2-1003-jdbc4
>>>>>> driver
>>>>>>
>>>>>> On 2013/11/05, at 16:09, Rui Pedro Leal wrote:
>>>>>>
>>>>>> > Hello everyone,
>>>>>> >
>>>>>> > I'm having slow JDBC performance on iterating a resultSet obtained
>>>>>> from a somewhat simple query.
>>>>>> >
>>>>>> > The query, although using PostGIS functions, is pretty straight
>>>>>> forward:
>>>>>> >
>>>>>> > SELECT _id, _spatial_type, ST_AsBinary(_geometry), _attribute,
>>>>>> _count, _references, countpersons, countfatals, countdrunks, density
>>>>>> > FROM accidents_5
>>>>>> > WHERE (_geometry &&
>>>>>> ST_MakeEnvelope(-126.60644531250001,26.43122806450644,-63.369140625,52.96187505907603,
>>>>>> 4326) )
>>>>>> > ORDER BY _pk_id
>>>>>> >
>>>>>> > The results are the following:
>>>>>> > - Executing the query directly from pgAdmin: ~2807ms
>>>>>> > - Executing from JVM + JDBC: 4184ms
>>>>>> >
>>>>>> > The code i'm executing is pretty much standard:
>>>>>> >
>>>>>> > -------------------- code --------------------
>>>>>> >
>>>>>> > public static void main(final String[] args) throws Exception
>>>>>> {
>>>>>> >
>>>>>> > String sql = "SELECT _id, _spatial_type,
>>>>>> ST_AsBinary(_geometry), _attribute, _count, _references, countpersons,
>>>>>> countfatals, countdrunks, density";
>>>>>> > sql += " FROM accidents_5";
>>>>>> > sql += " WHERE (_geometry &&
>>>>>> ST_MakeEnvelope(-126.60644531250001,26.43122806450644,-63.369140625,52.96187505907603,
>>>>>> 4326) )";
>>>>>> > sql += " ORDER BY _pk_id";
>>>>>> >
>>>>>> > System.out.println(sql);
>>>>>> >
>>>>>> > Long time = System.currentTimeMillis();
>>>>>> > try {
>>>>>> > Connection connection =
>>>>>> DataStoreInfo.getDataStores().get(0);
>>>>>> > connection.setAutoCommit(false);
>>>>>> >
>>>>>> > System.out.println("[QUERY " +
>>>>>> Thread.currentThread().getId()
>>>>>> > + "] - connection in "
>>>>>> > + (System.currentTimeMillis()
>>>>>> - time) + "ms.");
>>>>>> >
>>>>>> > Statement st = connection.createStatement(
>>>>>> > ResultSet.TYPE_FORWARD_ONLY,
>>>>>> ResultSet.CONCUR_READ_ONLY,
>>>>>> >
>>>>>> ResultSet.CLOSE_CURSORS_AT_COMMIT);
>>>>>> > st.setFetchSize(250);
>>>>>> >
>>>>>> > System.out.println("[QUERY " +
>>>>>> Thread.currentThread().getId()
>>>>>> > + "] - statement in " +
>>>>>> (System.currentTimeMillis() - time)
>>>>>> > + "ms.");
>>>>>> >
>>>>>> > ResultSet resultSet = st.executeQuery(sql);
>>>>>> >
>>>>>> > System.out.println("[QUERY " +
>>>>>> Thread.currentThread().getId()
>>>>>> > + "] - done in " +
>>>>>> (System.currentTimeMillis() - time)
>>>>>> > + "ms.");
>>>>>> >
>>>>>> > GeometryFactory geofact = new
>>>>>> GeometryFactory(new PrecisionModel(),4326);
>>>>>> > WKBReader wkbReader = new WKBReader(geofact);
>>>>>> >
>>>>>> > String id;
>>>>>> > String spatialType;
>>>>>> > Geometry geometry;
>>>>>> > String attribute;
>>>>>> > Long count;
>>>>>> > String reference;
>>>>>> > Map<String, Double> properties;
>>>>>> > String granularSynthString =
>>>>>> "GranularSynthesis";
>>>>>> >
>>>>>> > while (resultSet.next()) {
>>>>>> > id = resultSet.getString(1);
>>>>>> > spatialType = resultSet.getString(2);
>>>>>> >
>>>>>> > // geometry =
>>>>>> wkbReader.read(resultSet.getBytes(3)); // ignored the WKBReader and the
>>>>>> results are about the same.
>>>>>> > attribute = resultSet.getString(4);
>>>>>> > count = resultSet.getLong(5);
>>>>>> > reference = resultSet.getString(6);
>>>>>> >
>>>>>> > properties = new HashMap<String,
>>>>>> Double>();
>>>>>> > Double aux = resultSet.getDouble(7);
>>>>>> > properties.put("countpersons", aux);
>>>>>> > aux = resultSet.getDouble(8);
>>>>>> > properties.put("countfatals", aux);
>>>>>> > aux = resultSet.getDouble(9);
>>>>>> > properties.put("countdrunks", aux);
>>>>>> > aux = resultSet.getDouble(10);
>>>>>> > properties.put("density", aux);
>>>>>> > }
>>>>>> >
>>>>>> > System.out.println("[QUERY " +
>>>>>> Thread.currentThread().getId()
>>>>>> > + "] - done & iterated in "
>>>>>> > + (System.currentTimeMillis()
>>>>>> - time) + "ms.");
>>>>>> >
>>>>>> > resultSet.close();
>>>>>> > st.close();
>>>>>> > connection.commit();
>>>>>> > connection.close();
>>>>>> > } catch (SQLException exception) {
>>>>>> > exception.printStackTrace();
>>>>>> > // } catch (ParseException exception) { // ignored
>>>>>> from WKBreader
>>>>>> > // exception.printStackTrace();
>>>>>> > }
>>>>>> >
>>>>>> > System.out.println("[End " +
>>>>>> Thread.currentThread().getId()
>>>>>> > + "] - done in " +
>>>>>> (System.currentTimeMillis() - time) + "ms.");
>>>>>> > }
>>>>>> >
>>>>>> > -------------------- end code --------------------
>>>>>> >
>>>>>> > Although i'm executing this on slow MacbookPro (2.6 core duo, 2Gbs
>>>>>> RAM but SSD) and have a 9.1.2 postgres, i've also tested this on a recent
>>>>>> retina MBP and the ratio between pgAdmin and JDBC execution is similar.
>>>>>> >
>>>>>> > Is this expected? Can someone point if i'm doing something terrible
>>>>>> wrong?
>>>>>> >
>>>>>> > I'm not concerned about the query performance per-se (i know it CAN
>>>>>> be optimized), but the differences just using JDBC and iterating the
>>>>>> resultSet are really annoying.
>>>>>> >
>>>>>> > Thanks in advance for any help.
>>>>>> >
>>>>>> > Kind regards,
>>>>>> >
>>>>>> > Rui Leal
>>>>>> >
>>>>>> >
>>>>>>
>>>>>>
>>>>>>
>>>>>> --
>>>>>> Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
>>>>>> To make changes to your subscription:
>>>>>> http://www.postgresql.org/mailpref/pgsql-jdbc
>>>>>>
>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>
>>
>>
>
>
^ permalink raw reply [nested|flat] 29+ messages in thread
* Re: JDBC slow performance on resultSet iteration?
@ 2013-11-05 18:27 Rui Pedro Leal <rui.pedro.leal@gmail.com>
parent: Kevin Wooten <kdubb@me.com>
0 siblings, 1 reply; 29+ messages in thread
From: Rui Pedro Leal @ 2013-11-05 18:27 UTC (permalink / raw)
To: Kevin Wooten <kdubb@me.com>; +Cc: Dave Cramer <pg@fastcrypt.com>; pgsql-jdbc
Hello Kevin,
That's exactly my immediate (and hopefully wrong) conclusion: the getXXX calls are really costly!
So, you are proposing using the binary protocol. I've never known the existence of that. Is this it: http://wiki.postgresql.org/wiki/JDBC-BinaryTransfer ?
Thanks.
[] Rui Pedro Leal
[] rui.pedro.leal@gmail.com
On 2013/11/05, at 18:14, Kevin Wooten wrote:
> Well I can speak from experience…
>
> When I was comparing the performance of my implementation with pgjdbc I was very disheartened at the beginning as pgjdbc was seemingly much faster. It was only after I switched the test to take into account the getXXX calls that the true cost of using the driver was discovered. If you just execute a query and ignore the results pgjdbc is essentially just receiving a stream of text. It ignores all of the work of turning that text into Java objects/primitives.
>
> My implementation uses binary protocol for this reason and that removes a lot of the overhead that pgjdbc has. If pgAdmin is using the binary protocol as well this could very well be the difference that is being seen, because it is a significant amount of time.
>
> On Nov 5, 2013, at 11:03 AM, Dave Cramer <pg@fastcrypt.com> wrote:
>
>> I do not know, the best way to figure this out is to use explain analyze in pg_admin.
>>
>> What I am debating is the java code is using a cursor. pgAdmin is not.
>>
>> Try it without setFetchSize()
>>
>> Dave Cramer
>>
>> dave.cramer(at)credativ(dot)ca
>> http://www.credativ.ca
>>
>>
>> On Tue, Nov 5, 2013 at 1:00 PM, Rui Pedro Leal <rui.pedro.leal@gmail.com> wrote:
>> Dave,
>>
>> I understand what you are saying, but i must get the results, right? :)
>>
>> I think in pgAdmin the total query time, presented by the query execution windows, is also considering the rendering / presenting of the query results, so this is comparable ... right?
>>
>> [] Rui Pedro Leal
>> [] rui.pedro.leal@gmail.com
>>
>> On 2013/11/05, at 17:34, Dave Cramer wrote:
>>
>>> Rui Leal,
>>>
>>> You shouldn't see that much of a penatly, but I question how you are measuring it. Since you are timing the loop which is using cursors.
>>>
>>> Just run a simple query and time it without iterating through the result set. That would be equivalent
>>>
>>> Dave Cramer
>>>
>>> dave.cramer(at)credativ(dot)ca
>>> http://www.credativ.ca
>>>
>>>
>>> On Tue, Nov 5, 2013 at 12:16 PM, Rui Pedro Leal <rui.pedro.leal@gmail.com> wrote:
>>> Hello Dave, thanks for the quick reply.
>>>
>>> I'm just executing the same query on pgAdmin query interface. Just a way to measure performance of the query + getting results vs the JDBC way.
>>>
>>> I'm using fetchsize in order to get some faster results when iterating the resultSet. I've tried not to use fetchSize/cursor but the performance is naturally slower (and worser than the simple pgAdmin query).
>>>
>>> What i'm asking is .. is this performance penalty i'm getting using postgres JDBC normal and within what's expected? I'm i doing something wrong? Can i do it in any other way?
>>>
>>> Regards,
>>>
>>> Rui Leal
>>>
>>> [] Rui Pedro Leal
>>> [] rui.pedro.leal@gmail.com
>>>
>>>
>>> On 2013/11/05, at 16:43, Dave Cramer wrote:
>>>
>>>> How can you measure the same query in pgAdmin ? JDBC will use a cursor when you set the fetchsize.
>>>>
>>>> Dave Cramer
>>>>
>>>> dave.cramer(at)credativ(dot)ca
>>>> http://www.credativ.ca
>>>>
>>>>
>>>> On Tue, Nov 5, 2013 at 11:12 AM, Rui Pedro Leal <rui.pedro.leal@gmail.com> wrote:
>>>> Just an quick info update: i'm using the postgresql-9.2-1003-jdbc4 driver
>>>>
>>>> On 2013/11/05, at 16:09, Rui Pedro Leal wrote:
>>>>
>>>> > Hello everyone,
>>>> >
>>>> > I'm having slow JDBC performance on iterating a resultSet obtained from a somewhat simple query.
>>>> >
>>>> > The query, although using PostGIS functions, is pretty straight forward:
>>>> >
>>>> > SELECT _id, _spatial_type, ST_AsBinary(_geometry), _attribute, _count, _references, countpersons, countfatals, countdrunks, density
>>>> > FROM accidents_5
>>>> > WHERE (_geometry && ST_MakeEnvelope(-126.60644531250001,26.43122806450644,-63.369140625,52.96187505907603, 4326) )
>>>> > ORDER BY _pk_id
>>>> >
>>>> > The results are the following:
>>>> > - Executing the query directly from pgAdmin: ~2807ms
>>>> > - Executing from JVM + JDBC: 4184ms
>>>> >
>>>> > The code i'm executing is pretty much standard:
>>>> >
>>>> > -------------------- code --------------------
>>>> >
>>>> > public static void main(final String[] args) throws Exception {
>>>> >
>>>> > String sql = "SELECT _id, _spatial_type, ST_AsBinary(_geometry), _attribute, _count, _references, countpersons, countfatals, countdrunks, density";
>>>> > sql += " FROM accidents_5";
>>>> > sql += " WHERE (_geometry && ST_MakeEnvelope(-126.60644531250001,26.43122806450644,-63.369140625,52.96187505907603, 4326) )";
>>>> > sql += " ORDER BY _pk_id";
>>>> >
>>>> > System.out.println(sql);
>>>> >
>>>> > Long time = System.currentTimeMillis();
>>>> > try {
>>>> > Connection connection = DataStoreInfo.getDataStores().get(0);
>>>> > connection.setAutoCommit(false);
>>>> >
>>>> > System.out.println("[QUERY " + Thread.currentThread().getId()
>>>> > + "] - connection in "
>>>> > + (System.currentTimeMillis() - time) + "ms.");
>>>> >
>>>> > Statement st = connection.createStatement(
>>>> > ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY,
>>>> > ResultSet.CLOSE_CURSORS_AT_COMMIT);
>>>> > st.setFetchSize(250);
>>>> >
>>>> > System.out.println("[QUERY " + Thread.currentThread().getId()
>>>> > + "] - statement in " + (System.currentTimeMillis() - time)
>>>> > + "ms.");
>>>> >
>>>> > ResultSet resultSet = st.executeQuery(sql);
>>>> >
>>>> > System.out.println("[QUERY " + Thread.currentThread().getId()
>>>> > + "] - done in " + (System.currentTimeMillis() - time)
>>>> > + "ms.");
>>>> >
>>>> > GeometryFactory geofact = new GeometryFactory(new PrecisionModel(),4326);
>>>> > WKBReader wkbReader = new WKBReader(geofact);
>>>> >
>>>> > String id;
>>>> > String spatialType;
>>>> > Geometry geometry;
>>>> > String attribute;
>>>> > Long count;
>>>> > String reference;
>>>> > Map<String, Double> properties;
>>>> > String granularSynthString = "GranularSynthesis";
>>>> >
>>>> > while (resultSet.next()) {
>>>> > id = resultSet.getString(1);
>>>> > spatialType = resultSet.getString(2);
>>>> >
>>>> > // geometry = wkbReader.read(resultSet.getBytes(3)); // ignored the WKBReader and the results are about the same.
>>>> > attribute = resultSet.getString(4);
>>>> > count = resultSet.getLong(5);
>>>> > reference = resultSet.getString(6);
>>>> >
>>>> > properties = new HashMap<String, Double>();
>>>> > Double aux = resultSet.getDouble(7);
>>>> > properties.put("countpersons", aux);
>>>> > aux = resultSet.getDouble(8);
>>>> > properties.put("countfatals", aux);
>>>> > aux = resultSet.getDouble(9);
>>>> > properties.put("countdrunks", aux);
>>>> > aux = resultSet.getDouble(10);
>>>> > properties.put("density", aux);
>>>> > }
>>>> >
>>>> > System.out.println("[QUERY " + Thread.currentThread().getId()
>>>> > + "] - done & iterated in "
>>>> > + (System.currentTimeMillis() - time) + "ms.");
>>>> >
>>>> > resultSet.close();
>>>> > st.close();
>>>> > connection.commit();
>>>> > connection.close();
>>>> > } catch (SQLException exception) {
>>>> > exception.printStackTrace();
>>>> > // } catch (ParseException exception) { // ignored from WKBreader
>>>> > // exception.printStackTrace();
>>>> > }
>>>> >
>>>> > System.out.println("[End " + Thread.currentThread().getId()
>>>> > + "] - done in " + (System.currentTimeMillis() - time) + "ms.");
>>>> > }
>>>> >
>>>> > -------------------- end code --------------------
>>>> >
>>>> > Although i'm executing this on slow MacbookPro (2.6 core duo, 2Gbs RAM but SSD) and have a 9.1.2 postgres, i've also tested this on a recent retina MBP and the ratio between pgAdmin and JDBC execution is similar.
>>>> >
>>>> > Is this expected? Can someone point if i'm doing something terrible wrong?
>>>> >
>>>> > I'm not concerned about the query performance per-se (i know it CAN be optimized), but the differences just using JDBC and iterating the resultSet are really annoying.
>>>> >
>>>> > Thanks in advance for any help.
>>>> >
>>>> > Kind regards,
>>>> >
>>>> > Rui Leal
>>>> >
>>>> >
>>>>
>>>>
>>>>
>>>> --
>>>> Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
>>>> To make changes to your subscription:
>>>> http://www.postgresql.org/mailpref/pgsql-jdbc
>>>>
>>>
>>>
>>
>>
>
^ permalink raw reply [nested|flat] 29+ messages in thread
* Re: JDBC slow performance on resultSet iteration?
@ 2013-11-05 18:34 Rui Pedro Leal <rui.pedro.leal@gmail.com>
parent: Edson Richter <edsonrichter@hotmail.com>
0 siblings, 0 replies; 29+ messages in thread
From: Rui Pedro Leal @ 2013-11-05 18:34 UTC (permalink / raw)
To: Edson Richter <edsonrichter@hotmail.com>; +Cc: pgsql-jdbc
Hi Edson,
In fact i'm measuring on various steps as you can see by the code.
Where is a print of a simple execution .... this time with preparedStatement and fetchSize (250):
[QUERY 12] - connection in 7ms.
[QUERY 12] - statement in 7ms.
[QUERY 12] - done in 43ms.
[QUERY 12] - done & iterated in 4195ms.
And check the results just using a createStatement and no fetchSize/cursor:
[QUERY 12] - connection in 7ms.
[QUERY 12] - statement in 7ms.
[QUERY 12] - done in 3210ms.
[QUERY 12] - done & iterated in 4619ms.
The same query in pgAdmin is giving me the results (query + presenting / rendering) in 1601ms.
[] Rui Pedro Leal
[] rui.pedro.leal@gmail.com
On 2013/11/05, at 18:14, Edson Richter wrote:
> Pedro,
>
> Would worth to change the "Long time = System.currentTimeMillis();" to just before the getResultSet(), otherwise other variables would be affecting results... like prepare time (and also initial connection).
>
> Edson
>
> Em 05/11/2013 16:05, Rui Pedro Leal escreveu:
>> Hello Edson.
>>
>> I'm running everything locally on a laptop. I simply connect to PG via JDBC localhost. Just a simple development setup.
>>
>> [] Rui Pedro Leal
>> [] rui.pedro.leal@gmail.com
>>
>> On 2013/11/05, at 17:47, Edson Richter wrote:
>>
>>> Are you using a network between your station and the server?
>>> You're connection using IP address or server name?
>>> If the client machine has to access server several times (like in a cursor scenario), would be necessary to solve machine names, which could slow down the whole process.
>>>
>>> Regards,
>>>
>>> Edson
>>
>>> Em 05/11/2013 15:42, Dave Cramer escreveu:
>>>> Kevin,
>>>>
>>>> Ya, however I am still questioning the basic time measurement.
>>>>
>>>> Dave Cramer
>>>>
>>>> dave.cramer(at)credativ(dot)ca
>>>> http://www.credativ.ca
>>>>
>>>>
>>>> On Tue, Nov 5, 2013 at 12:39 PM, Kevin Wooten <kdubb@me.com> wrote:
>>>> Dave,
>>>> That is not necessarily true. Most of the drivers data conversion is done in the getXXX calls of the ResultSet. All of which would count as “penalty”. I’m not saying it’s a large penalty, just that this is time that must be considered since pgAdmin is converting and displaying the data as well.
>>>>
>>>> On Nov 5, 2013, at 10:34 AM, Dave Cramer <pg@fastcrypt.com> wrote:
>>>>
>>>>> Rui Leal,
>>>>>
>>>>> You shouldn't see that much of a penatly, but I question how you are measuring it. Since you are timing the loop which is using cursors.
>>>>>
>>>>> Just run a simple query and time it without iterating through the result set. That would be equivalent
>>>>>
>>>>> Dave Cramer
>>>>>
>>>>> dave.cramer(at)credativ(dot)ca
>>>>> http://www.credativ.ca
>>>>>
>>>>>
>>>>> On Tue, Nov 5, 2013 at 12:16 PM, Rui Pedro Leal <rui.pedro.leal@gmail.com> wrote:
>>>>> Hello Dave, thanks for the quick reply.
>>>>>
>>>>> I'm just executing the same query on pgAdmin query interface. Just a way to measure performance of the query + getting results vs the JDBC way.
>>>>>
>>>>> I'm using fetchsize in order to get some faster results when iterating the resultSet. I've tried not to use fetchSize/cursor but the performance is naturally slower (and worser than the simple pgAdmin query).
>>>>>
>>>>> What i'm asking is .. is this performance penalty i'm getting using postgres JDBC normal and within what's expected? I'm i doing something wrong? Can i do it in any other way?
>>>>>
>>>>> Regards,
>>>>>
>>>>> Rui Leal
>>>>>
>>>>> [] Rui Pedro Leal
>>>>> [] rui.pedro.leal@gmail.com
>>>>>
>>>>>
>>>>> On 2013/11/05, at 16:43, Dave Cramer wrote:
>>>>>
>>>>>> How can you measure the same query in pgAdmin ? JDBC will use a cursor when you set the fetchsize.
>>>>>>
>>>>>> Dave Cramer
>>>>>>
>>>>>> dave.cramer(at)credativ(dot)ca
>>>>>> http://www.credativ.ca
>>>>>>
>>>>>>
>>>>>> On Tue, Nov 5, 2013 at 11:12 AM, Rui Pedro Leal <rui.pedro.leal@gmail.com> wrote:
>>>>>> Just an quick info update: i'm using the postgresql-9.2-1003-jdbc4 driver
>>>>>>
>>>>>> On 2013/11/05, at 16:09, Rui Pedro Leal wrote:
>>>>>>
>>>>>> > Hello everyone,
>>>>>> >
>>>>>> > I'm having slow JDBC performance on iterating a resultSet obtained from a somewhat simple query.
>>>>>> >
>>>>>> > The query, although using PostGIS functions, is pretty straight forward:
>>>>>> >
>>>>>> > SELECT _id, _spatial_type, ST_AsBinary(_geometry), _attribute, _count, _references, countpersons, countfatals, countdrunks, density
>>>>>> > FROM accidents_5
>>>>>> > WHERE (_geometry && ST_MakeEnvelope(-126.60644531250001,26.43122806450644,-63.369140625,52.96187505907603, 4326) )
>>>>>> > ORDER BY _pk_id
>>>>>> >
>>>>>> > The results are the following:
>>>>>> > - Executing the query directly from pgAdmin: ~2807ms
>>>>>> > - Executing from JVM + JDBC: 4184ms
>>>>>> >
>>>>>> > The code i'm executing is pretty much standard:
>>>>>> >
>>>>>> > -------------------- code --------------------
>>>>>> >
>>>>>> > public static void main(final String[] args) throws Exception {
>>>>>> >
>>>>>> > String sql = "SELECT _id, _spatial_type, ST_AsBinary(_geometry), _attribute, _count, _references, countpersons, countfatals, countdrunks, density";
>>>>>> > sql += " FROM accidents_5";
>>>>>> > sql += " WHERE (_geometry && ST_MakeEnvelope(-126.60644531250001,26.43122806450644,-63.369140625,52.96187505907603, 4326) )";
>>>>>> > sql += " ORDER BY _pk_id";
>>>>>> >
>>>>>> > System.out.println(sql);
>>>>>> >
>>>>>> > Long time = System.currentTimeMillis();
>>>>>> > try {
>>>>>> > Connection connection = DataStoreInfo.getDataStores().get(0);
>>>>>> > connection.setAutoCommit(false);
>>>>>> >
>>>>>> > System.out.println("[QUERY " + Thread.currentThread().getId()
>>>>>> > + "] - connection in "
>>>>>> > + (System.currentTimeMillis() - time) + "ms.");
>>>>>> >
>>>>>> > Statement st = connection.createStatement(
>>>>>> > ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY,
>>>>>> > ResultSet.CLOSE_CURSORS_AT_COMMIT);
>>>>>> > st.setFetchSize(250);
>>>>>> >
>>>>>> > System.out.println("[QUERY " + Thread.currentThread().getId()
>>>>>> > + "] - statement in " + (System.currentTimeMillis() - time)
>>>>>> > + "ms.");
>>>>>> >
>>>>>> > ResultSet resultSet = st.executeQuery(sql);
>>>>>> >
>>>>>> > System.out.println("[QUERY " + Thread.currentThread().getId()
>>>>>> > + "] - done in " + (System.currentTimeMillis() - time)
>>>>>> > + "ms.");
>>>>>> >
>>>>>> > GeometryFactory geofact = new GeometryFactory(new PrecisionModel(),4326);
>>>>>> > WKBReader wkbReader = new WKBReader(geofact);
>>>>>> >
>>>>>> > String id;
>>>>>> > String spatialType;
>>>>>> > Geometry geometry;
>>>>>> > String attribute;
>>>>>> > Long count;
>>>>>> > String reference;
>>>>>> > Map<String, Double> properties;
>>>>>> > String granularSynthString = "GranularSynthesis";
>>>>>> >
>>>>>> > while (resultSet.next()) {
>>>>>> > id = resultSet.getString(1);
>>>>>> > spatialType = resultSet.getString(2);
>>>>>> >
>>>>>> > // geometry = wkbReader.read(resultSet.getBytes(3)); // ignored the WKBReader and the results are about the same.
>>>>>> > attribute = resultSet.getString(4);
>>>>>> > count = resultSet.getLong(5);
>>>>>> > reference = resultSet.getString(6);
>>>>>> >
>>>>>> > properties = new HashMap<String, Double>();
>>>>>> > Double aux = resultSet.getDouble(7);
>>>>>> > properties.put("countpersons", aux);
>>>>>> > aux = resultSet.getDouble(8);
>>>>>> > properties.put("countfatals", aux);
>>>>>> > aux = resultSet.getDouble(9);
>>>>>> > properties.put("countdrunks", aux);
>>>>>> > aux = resultSet.getDouble(10);
>>>>>> > properties.put("density", aux);
>>>>>> > }
>>>>>> >
>>>>>> > System.out.println("[QUERY " + Thread.currentThread().getId()
>>>>>> > + "] - done & iterated in "
>>>>>> > + (System.currentTimeMillis() - time) + "ms.");
>>>>>> >
>>>>>> > resultSet.close();
>>>>>> > st.close();
>>>>>> > connection.commit();
>>>>>> > connection.close();
>>>>>> > } catch (SQLException exception) {
>>>>>> > exception.printStackTrace();
>>>>>> > // } catch (ParseException exception) { // ignored from WKBreader
>>>>>> > // exception.printStackTrace();
>>>>>> > }
>>>>>> >
>>>>>> > System.out.println("[End " + Thread.currentThread().getId()
>>>>>> > + "] - done in " + (System.currentTimeMillis() - time) + "ms.");
>>>>>> > }
>>>>>> >
>>>>>> > -------------------- end code --------------------
>>>>>> >
>>>>>> > Although i'm executing this on slow MacbookPro (2.6 core duo, 2Gbs RAM but SSD) and have a 9.1.2 postgres, i've also tested this on a recent retina MBP and the ratio between pgAdmin and JDBC execution is similar.
>>>>>> >
>>>>>> > Is this expected? Can someone point if i'm doing something terrible wrong?
>>>>>> >
>>>>>> > I'm not concerned about the query performance per-se (i know it CAN be optimized), but the differences just using JDBC and iterating the resultSet are really annoying.
>>>>>> >
>>>>>> > Thanks in advance for any help.
>>>>>> >
>>>>>> > Kind regards,
>>>>>> >
>>>>>> > Rui Leal
>>>>>> >
>>>>>> >
>>>>>>
>>>>>>
>>>>>>
>>>>>> --
>>>>>> Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
>>>>>> To make changes to your subscription:
>>>>>> http://www.postgresql.org/mailpref/pgsql-jdbc
>>>>>>
>>>>>
>>>>>
>>>>
>>
^ permalink raw reply [nested|flat] 29+ messages in thread
* Re: JDBC slow performance on resultSet iteration?
@ 2013-11-05 18:34 Kevin Wooten <kdubb@me.com>
parent: Dave Cramer <pg@fastcrypt.com>
0 siblings, 0 replies; 29+ messages in thread
From: Kevin Wooten @ 2013-11-05 18:34 UTC (permalink / raw)
To: Dave Cramer <pg@fastcrypt.com>; +Cc: pgsql-jdbc
In the FE/BE protocol the Simple Query can only transfer in text. Doesn’t the driver use the simple query protocol in a lot of cases or is it always using the bind/exec version? Again, I may very well be misremembering how it works.
On Nov 5, 2013, at 11:25 AM, Dave Cramer <pg@fastcrypt.com> wrote:
> essentially everything is a prepared statement, and AFAIK, it is using binary protocol
>
> Dave Cramer
>
> dave.cramer(at)credativ(dot)ca
> http://www.credativ.ca
>
>
> On Tue, Nov 5, 2013 at 1:23 PM, Kevin Wooten <kdubb@me.com> wrote:
> Am I remembering incorrectly? I thought the driver only used binary for a few types and only if it was a prepared statement?
>
> On Nov 5, 2013, at 11:18 AM, Dave Cramer <pg@fastcrypt.com> wrote:
>
>> I'd still like to know just how bad the getXXX calls are. Also the driver is using binary protocol, so it shouldn't be that bad
>>
>> Dave Cramer
>>
>> dave.cramer(at)credativ(dot)ca
>> http://www.credativ.ca
>>
>>
>> On Tue, Nov 5, 2013 at 1:16 PM, Kevin Wooten <kdubb@me.com> wrote:
>> Dave,
>>
>> I don’t believe he is not asking about the query time. He is specifically asking about the cost of using JDBC vs whatever pgAdmin is using (which seems to be libpq).
>>
>>
>> On Nov 5, 2013, at 11:08 AM, Dave Cramer <pg@fastcrypt.com> wrote:
>>
>>> Rui,
>>>
>>> The best way to settle this is to turn on log_duration in the server and get the server timings. That will give you the query time without rendering
>>>
>>> Dave Cramer
>>>
>>> dave.cramer(at)credativ(dot)ca
>>> http://www.credativ.ca
>>>
>>>
>>> On Tue, Nov 5, 2013 at 1:05 PM, Rui Pedro Leal <rui.pedro.leal@gmail.com> wrote:
>>> Hello Edson.
>>>
>>> I'm running everything locally on a laptop. I simply connect to PG via JDBC localhost. Just a simple development setup.
>>>
>>> [] Rui Pedro Leal
>>> [] rui.pedro.leal@gmail.com
>>>
>>> On 2013/11/05, at 17:47, Edson Richter wrote:
>>>
>>>> Are you using a network between your station and the server?
>>>> You're connection using IP address or server name?
>>>> If the client machine has to access server several times (like in a cursor scenario), would be necessary to solve machine names, which could slow down the whole process.
>>>>
>>>> Regards,
>>>>
>>>> Edson
>>>
>>>> Em 05/11/2013 15:42, Dave Cramer escreveu:
>>>>> Kevin,
>>>>>
>>>>> Ya, however I am still questioning the basic time measurement.
>>>>>
>>>>> Dave Cramer
>>>>>
>>>>> dave.cramer(at)credativ(dot)ca
>>>>> http://www.credativ.ca
>>>>>
>>>>>
>>>>> On Tue, Nov 5, 2013 at 12:39 PM, Kevin Wooten <kdubb@me.com> wrote:
>>>>> Dave,
>>>>> That is not necessarily true. Most of the drivers data conversion is done in the getXXX calls of the ResultSet. All of which would count as “penalty”. I’m not saying it’s a large penalty, just that this is time that must be considered since pgAdmin is converting and displaying the data as well.
>>>>>
>>>>> On Nov 5, 2013, at 10:34 AM, Dave Cramer <pg@fastcrypt.com> wrote:
>>>>>
>>>>>> Rui Leal,
>>>>>>
>>>>>> You shouldn't see that much of a penatly, but I question how you are measuring it. Since you are timing the loop which is using cursors.
>>>>>>
>>>>>> Just run a simple query and time it without iterating through the result set. That would be equivalent
>>>>>>
>>>>>> Dave Cramer
>>>>>>
>>>>>> dave.cramer(at)credativ(dot)ca
>>>>>> http://www.credativ.ca
>>>>>>
>>>>>>
>>>>>> On Tue, Nov 5, 2013 at 12:16 PM, Rui Pedro Leal <rui.pedro.leal@gmail.com> wrote:
>>>>>> Hello Dave, thanks for the quick reply.
>>>>>>
>>>>>> I'm just executing the same query on pgAdmin query interface. Just a way to measure performance of the query + getting results vs the JDBC way.
>>>>>>
>>>>>> I'm using fetchsize in order to get some faster results when iterating the resultSet. I've tried not to use fetchSize/cursor but the performance is naturally slower (and worser than the simple pgAdmin query).
>>>>>>
>>>>>> What i'm asking is .. is this performance penalty i'm getting using postgres JDBC normal and within what's expected? I'm i doing something wrong? Can i do it in any other way?
>>>>>>
>>>>>> Regards,
>>>>>>
>>>>>> Rui Leal
>>>>>>
>>>>>> [] Rui Pedro Leal
>>>>>> [] rui.pedro.leal@gmail.com
>>>>>>
>>>>>>
>>>>>> On 2013/11/05, at 16:43, Dave Cramer wrote:
>>>>>>
>>>>>>> How can you measure the same query in pgAdmin ? JDBC will use a cursor when you set the fetchsize.
>>>>>>>
>>>>>>> Dave Cramer
>>>>>>>
>>>>>>> dave.cramer(at)credativ(dot)ca
>>>>>>> http://www.credativ.ca
>>>>>>>
>>>>>>>
>>>>>>> On Tue, Nov 5, 2013 at 11:12 AM, Rui Pedro Leal <rui.pedro.leal@gmail.com> wrote:
>>>>>>> Just an quick info update: i'm using the postgresql-9.2-1003-jdbc4 driver
>>>>>>>
>>>>>>> On 2013/11/05, at 16:09, Rui Pedro Leal wrote:
>>>>>>>
>>>>>>> > Hello everyone,
>>>>>>> >
>>>>>>> > I'm having slow JDBC performance on iterating a resultSet obtained from a somewhat simple query.
>>>>>>> >
>>>>>>> > The query, although using PostGIS functions, is pretty straight forward:
>>>>>>> >
>>>>>>> > SELECT _id, _spatial_type, ST_AsBinary(_geometry), _attribute, _count, _references, countpersons, countfatals, countdrunks, density
>>>>>>> > FROM accidents_5
>>>>>>> > WHERE (_geometry && ST_MakeEnvelope(-126.60644531250001,26.43122806450644,-63.369140625,52.96187505907603, 4326) )
>>>>>>> > ORDER BY _pk_id
>>>>>>> >
>>>>>>> > The results are the following:
>>>>>>> > - Executing the query directly from pgAdmin: ~2807ms
>>>>>>> > - Executing from JVM + JDBC: 4184ms
>>>>>>> >
>>>>>>> > The code i'm executing is pretty much standard:
>>>>>>> >
>>>>>>> > -------------------- code --------------------
>>>>>>> >
>>>>>>> > public static void main(final String[] args) throws Exception {
>>>>>>> >
>>>>>>> > String sql = "SELECT _id, _spatial_type, ST_AsBinary(_geometry), _attribute, _count, _references, countpersons, countfatals, countdrunks, density";
>>>>>>> > sql += " FROM accidents_5";
>>>>>>> > sql += " WHERE (_geometry && ST_MakeEnvelope(-126.60644531250001,26.43122806450644,-63.369140625,52.96187505907603, 4326) )";
>>>>>>> > sql += " ORDER BY _pk_id";
>>>>>>> >
>>>>>>> > System.out.println(sql);
>>>>>>> >
>>>>>>> > Long time = System.currentTimeMillis();
>>>>>>> > try {
>>>>>>> > Connection connection = DataStoreInfo.getDataStores().get(0);
>>>>>>> > connection.setAutoCommit(false);
>>>>>>> >
>>>>>>> > System.out.println("[QUERY " + Thread.currentThread().getId()
>>>>>>> > + "] - connection in "
>>>>>>> > + (System.currentTimeMillis() - time) + "ms.");
>>>>>>> >
>>>>>>> > Statement st = connection.createStatement(
>>>>>>> > ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY,
>>>>>>> > ResultSet.CLOSE_CURSORS_AT_COMMIT);
>>>>>>> > st.setFetchSize(250);
>>>>>>> >
>>>>>>> > System.out.println("[QUERY " + Thread.currentThread().getId()
>>>>>>> > + "] - statement in " + (System.currentTimeMillis() - time)
>>>>>>> > + "ms.");
>>>>>>> >
>>>>>>> > ResultSet resultSet = st.executeQuery(sql);
>>>>>>> >
>>>>>>> > System.out.println("[QUERY " + Thread.currentThread().getId()
>>>>>>> > + "] - done in " + (System.currentTimeMillis() - time)
>>>>>>> > + "ms.");
>>>>>>> >
>>>>>>> > GeometryFactory geofact = new GeometryFactory(new PrecisionModel(),4326);
>>>>>>> > WKBReader wkbReader = new WKBReader(geofact);
>>>>>>> >
>>>>>>> > String id;
>>>>>>> > String spatialType;
>>>>>>> > Geometry geometry;
>>>>>>> > String attribute;
>>>>>>> > Long count;
>>>>>>> > String reference;
>>>>>>> > Map<String, Double> properties;
>>>>>>> > String granularSynthString = "GranularSynthesis";
>>>>>>> >
>>>>>>> > while (resultSet.next()) {
>>>>>>> > id = resultSet.getString(1);
>>>>>>> > spatialType = resultSet.getString(2);
>>>>>>> >
>>>>>>> > // geometry = wkbReader.read(resultSet.getBytes(3)); // ignored the WKBReader and the results are about the same.
>>>>>>> > attribute = resultSet.getString(4);
>>>>>>> > count = resultSet.getLong(5);
>>>>>>> > reference = resultSet.getString(6);
>>>>>>> >
>>>>>>> > properties = new HashMap<String, Double>();
>>>>>>> > Double aux = resultSet.getDouble(7);
>>>>>>> > properties.put("countpersons", aux);
>>>>>>> > aux = resultSet.getDouble(8);
>>>>>>> > properties.put("countfatals", aux);
>>>>>>> > aux = resultSet.getDouble(9);
>>>>>>> > properties.put("countdrunks", aux);
>>>>>>> > aux = resultSet.getDouble(10);
>>>>>>> > properties.put("density", aux);
>>>>>>> > }
>>>>>>> >
>>>>>>> > System.out.println("[QUERY " + Thread.currentThread().getId()
>>>>>>> > + "] - done & iterated in "
>>>>>>> > + (System.currentTimeMillis() - time) + "ms.");
>>>>>>> >
>>>>>>> > resultSet.close();
>>>>>>> > st.close();
>>>>>>> > connection.commit();
>>>>>>> > connection.close();
>>>>>>> > } catch (SQLException exception) {
>>>>>>> > exception.printStackTrace();
>>>>>>> > // } catch (ParseException exception) { // ignored from WKBreader
>>>>>>> > // exception.printStackTrace();
>>>>>>> > }
>>>>>>> >
>>>>>>> > System.out.println("[End " + Thread.currentThread().getId()
>>>>>>> > + "] - done in " + (System.currentTimeMillis() - time) + "ms.");
>>>>>>> > }
>>>>>>> >
>>>>>>> > -------------------- end code --------------------
>>>>>>> >
>>>>>>> > Although i'm executing this on slow MacbookPro (2.6 core duo, 2Gbs RAM but SSD) and have a 9.1.2 postgres, i've also tested this on a recent retina MBP and the ratio between pgAdmin and JDBC execution is similar.
>>>>>>> >
>>>>>>> > Is this expected? Can someone point if i'm doing something terrible wrong?
>>>>>>> >
>>>>>>> > I'm not concerned about the query performance per-se (i know it CAN be optimized), but the differences just using JDBC and iterating the resultSet are really annoying.
>>>>>>> >
>>>>>>> > Thanks in advance for any help.
>>>>>>> >
>>>>>>> > Kind regards,
>>>>>>> >
>>>>>>> > Rui Leal
>>>>>>> >
>>>>>>> >
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> --
>>>>>>> Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
>>>>>>> To make changes to your subscription:
>>>>>>> http://www.postgresql.org/mailpref/pgsql-jdbc
>>>>>>>
>>>>>>
>>>>>>
>>>>>
>>>
>>>
>>
>>
>
>
^ permalink raw reply [nested|flat] 29+ messages in thread
* Re: JDBC slow performance on resultSet iteration?
@ 2013-11-05 18:45 Kevin Wooten <kdubb@me.com>
parent: Rui Pedro Leal <rui.pedro.leal@gmail.com>
0 siblings, 2 replies; 29+ messages in thread
From: Kevin Wooten @ 2013-11-05 18:45 UTC (permalink / raw)
To: Rui Pedro Leal <rui.pedro.leal@gmail.com>; +Cc: Dave Cramer <pg@fastcrypt.com>; pgsql-jdbc
Well yes and no. The driver may be transferring your data in binary already but we should work out if that is actually true. I believe that option "-Dorg.postgresql.forcebinary=boolean” still works and forces an Extended Query which will allow binary transfers.
Try running with that option and see if it changes the timings.
On Nov 5, 2013, at 11:27 AM, Rui Pedro Leal <rui.pedro.leal@gmail.com> wrote:
> Hello Kevin,
>
> That's exactly my immediate (and hopefully wrong) conclusion: the getXXX calls are really costly!
>
> So, you are proposing using the binary protocol. I've never known the existence of that. Is this it: http://wiki.postgresql.org/wiki/JDBC-BinaryTransfer ?
>
> Thanks.
>
> [] Rui Pedro Leal
> [] rui.pedro.leal@gmail.com
>
> On 2013/11/05, at 18:14, Kevin Wooten wrote:
>
>> Well I can speak from experience…
>>
>> When I was comparing the performance of my implementation with pgjdbc I was very disheartened at the beginning as pgjdbc was seemingly much faster. It was only after I switched the test to take into account the getXXX calls that the true cost of using the driver was discovered. If you just execute a query and ignore the results pgjdbc is essentially just receiving a stream of text. It ignores all of the work of turning that text into Java objects/primitives.
>>
>> My implementation uses binary protocol for this reason and that removes a lot of the overhead that pgjdbc has. If pgAdmin is using the binary protocol as well this could very well be the difference that is being seen, because it is a significant amount of time.
>>
>> On Nov 5, 2013, at 11:03 AM, Dave Cramer <pg@fastcrypt.com> wrote:
>>
>>> I do not know, the best way to figure this out is to use explain analyze in pg_admin.
>>>
>>> What I am debating is the java code is using a cursor. pgAdmin is not.
>>>
>>> Try it without setFetchSize()
>>>
>>> Dave Cramer
>>>
>>> dave.cramer(at)credativ(dot)ca
>>> http://www.credativ.ca
>>>
>>>
>>> On Tue, Nov 5, 2013 at 1:00 PM, Rui Pedro Leal <rui.pedro.leal@gmail.com> wrote:
>>> Dave,
>>>
>>> I understand what you are saying, but i must get the results, right? :)
>>>
>>> I think in pgAdmin the total query time, presented by the query execution windows, is also considering the rendering / presenting of the query results, so this is comparable ... right?
>>>
>>> [] Rui Pedro Leal
>>> [] rui.pedro.leal@gmail.com
>>>
>>> On 2013/11/05, at 17:34, Dave Cramer wrote:
>>>
>>>> Rui Leal,
>>>>
>>>> You shouldn't see that much of a penatly, but I question how you are measuring it. Since you are timing the loop which is using cursors.
>>>>
>>>> Just run a simple query and time it without iterating through the result set. That would be equivalent
>>>>
>>>> Dave Cramer
>>>>
>>>> dave.cramer(at)credativ(dot)ca
>>>> http://www.credativ.ca
>>>>
>>>>
>>>> On Tue, Nov 5, 2013 at 12:16 PM, Rui Pedro Leal <rui.pedro.leal@gmail.com> wrote:
>>>> Hello Dave, thanks for the quick reply.
>>>>
>>>> I'm just executing the same query on pgAdmin query interface. Just a way to measure performance of the query + getting results vs the JDBC way.
>>>>
>>>> I'm using fetchsize in order to get some faster results when iterating the resultSet. I've tried not to use fetchSize/cursor but the performance is naturally slower (and worser than the simple pgAdmin query).
>>>>
>>>> What i'm asking is .. is this performance penalty i'm getting using postgres JDBC normal and within what's expected? I'm i doing something wrong? Can i do it in any other way?
>>>>
>>>> Regards,
>>>>
>>>> Rui Leal
>>>>
>>>> [] Rui Pedro Leal
>>>> [] rui.pedro.leal@gmail.com
>>>>
>>>>
>>>> On 2013/11/05, at 16:43, Dave Cramer wrote:
>>>>
>>>>> How can you measure the same query in pgAdmin ? JDBC will use a cursor when you set the fetchsize.
>>>>>
>>>>> Dave Cramer
>>>>>
>>>>> dave.cramer(at)credativ(dot)ca
>>>>> http://www.credativ.ca
>>>>>
>>>>>
>>>>> On Tue, Nov 5, 2013 at 11:12 AM, Rui Pedro Leal <rui.pedro.leal@gmail.com> wrote:
>>>>> Just an quick info update: i'm using the postgresql-9.2-1003-jdbc4 driver
>>>>>
>>>>> On 2013/11/05, at 16:09, Rui Pedro Leal wrote:
>>>>>
>>>>> > Hello everyone,
>>>>> >
>>>>> > I'm having slow JDBC performance on iterating a resultSet obtained from a somewhat simple query.
>>>>> >
>>>>> > The query, although using PostGIS functions, is pretty straight forward:
>>>>> >
>>>>> > SELECT _id, _spatial_type, ST_AsBinary(_geometry), _attribute, _count, _references, countpersons, countfatals, countdrunks, density
>>>>> > FROM accidents_5
>>>>> > WHERE (_geometry && ST_MakeEnvelope(-126.60644531250001,26.43122806450644,-63.369140625,52.96187505907603, 4326) )
>>>>> > ORDER BY _pk_id
>>>>> >
>>>>> > The results are the following:
>>>>> > - Executing the query directly from pgAdmin: ~2807ms
>>>>> > - Executing from JVM + JDBC: 4184ms
>>>>> >
>>>>> > The code i'm executing is pretty much standard:
>>>>> >
>>>>> > -------------------- code --------------------
>>>>> >
>>>>> > public static void main(final String[] args) throws Exception {
>>>>> >
>>>>> > String sql = "SELECT _id, _spatial_type, ST_AsBinary(_geometry), _attribute, _count, _references, countpersons, countfatals, countdrunks, density";
>>>>> > sql += " FROM accidents_5";
>>>>> > sql += " WHERE (_geometry && ST_MakeEnvelope(-126.60644531250001,26.43122806450644,-63.369140625,52.96187505907603, 4326) )";
>>>>> > sql += " ORDER BY _pk_id";
>>>>> >
>>>>> > System.out.println(sql);
>>>>> >
>>>>> > Long time = System.currentTimeMillis();
>>>>> > try {
>>>>> > Connection connection = DataStoreInfo.getDataStores().get(0);
>>>>> > connection.setAutoCommit(false);
>>>>> >
>>>>> > System.out.println("[QUERY " + Thread.currentThread().getId()
>>>>> > + "] - connection in "
>>>>> > + (System.currentTimeMillis() - time) + "ms.");
>>>>> >
>>>>> > Statement st = connection.createStatement(
>>>>> > ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY,
>>>>> > ResultSet.CLOSE_CURSORS_AT_COMMIT);
>>>>> > st.setFetchSize(250);
>>>>> >
>>>>> > System.out.println("[QUERY " + Thread.currentThread().getId()
>>>>> > + "] - statement in " + (System.currentTimeMillis() - time)
>>>>> > + "ms.");
>>>>> >
>>>>> > ResultSet resultSet = st.executeQuery(sql);
>>>>> >
>>>>> > System.out.println("[QUERY " + Thread.currentThread().getId()
>>>>> > + "] - done in " + (System.currentTimeMillis() - time)
>>>>> > + "ms.");
>>>>> >
>>>>> > GeometryFactory geofact = new GeometryFactory(new PrecisionModel(),4326);
>>>>> > WKBReader wkbReader = new WKBReader(geofact);
>>>>> >
>>>>> > String id;
>>>>> > String spatialType;
>>>>> > Geometry geometry;
>>>>> > String attribute;
>>>>> > Long count;
>>>>> > String reference;
>>>>> > Map<String, Double> properties;
>>>>> > String granularSynthString = "GranularSynthesis";
>>>>> >
>>>>> > while (resultSet.next()) {
>>>>> > id = resultSet.getString(1);
>>>>> > spatialType = resultSet.getString(2);
>>>>> >
>>>>> > // geometry = wkbReader.read(resultSet.getBytes(3)); // ignored the WKBReader and the results are about the same.
>>>>> > attribute = resultSet.getString(4);
>>>>> > count = resultSet.getLong(5);
>>>>> > reference = resultSet.getString(6);
>>>>> >
>>>>> > properties = new HashMap<String, Double>();
>>>>> > Double aux = resultSet.getDouble(7);
>>>>> > properties.put("countpersons", aux);
>>>>> > aux = resultSet.getDouble(8);
>>>>> > properties.put("countfatals", aux);
>>>>> > aux = resultSet.getDouble(9);
>>>>> > properties.put("countdrunks", aux);
>>>>> > aux = resultSet.getDouble(10);
>>>>> > properties.put("density", aux);
>>>>> > }
>>>>> >
>>>>> > System.out.println("[QUERY " + Thread.currentThread().getId()
>>>>> > + "] - done & iterated in "
>>>>> > + (System.currentTimeMillis() - time) + "ms.");
>>>>> >
>>>>> > resultSet.close();
>>>>> > st.close();
>>>>> > connection.commit();
>>>>> > connection.close();
>>>>> > } catch (SQLException exception) {
>>>>> > exception.printStackTrace();
>>>>> > // } catch (ParseException exception) { // ignored from WKBreader
>>>>> > // exception.printStackTrace();
>>>>> > }
>>>>> >
>>>>> > System.out.println("[End " + Thread.currentThread().getId()
>>>>> > + "] - done in " + (System.currentTimeMillis() - time) + "ms.");
>>>>> > }
>>>>> >
>>>>> > -------------------- end code --------------------
>>>>> >
>>>>> > Although i'm executing this on slow MacbookPro (2.6 core duo, 2Gbs RAM but SSD) and have a 9.1.2 postgres, i've also tested this on a recent retina MBP and the ratio between pgAdmin and JDBC execution is similar.
>>>>> >
>>>>> > Is this expected? Can someone point if i'm doing something terrible wrong?
>>>>> >
>>>>> > I'm not concerned about the query performance per-se (i know it CAN be optimized), but the differences just using JDBC and iterating the resultSet are really annoying.
>>>>> >
>>>>> > Thanks in advance for any help.
>>>>> >
>>>>> > Kind regards,
>>>>> >
>>>>> > Rui Leal
>>>>> >
>>>>> >
>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
>>>>> To make changes to your subscription:
>>>>> http://www.postgresql.org/mailpref/pgsql-jdbc
>>>>>
>>>>
>>>>
>>>
>>>
>>
>
^ permalink raw reply [nested|flat] 29+ messages in thread
* Re: JDBC slow performance on resultSet iteration?
@ 2013-11-05 18:50 Edson Richter <edsonrichter@hotmail.com>
parent: Kevin Wooten <kdubb@me.com>
1 sibling, 1 reply; 29+ messages in thread
From: Edson Richter @ 2013-11-05 18:50 UTC (permalink / raw)
To: pgsql-jdbc
Em 05/11/2013 16:45, Kevin Wooten escreveu:
> Well yes and no. The driver may be transferring your data in binary
> already but we should work out if that is actually true. I believe
> that option "-Dorg.postgresql.forcebinary=boolean" still works and
> forces an Extended Query which will allow binary transfers.
>
> Try running with that option and see if it changes the timings.
>
> On Nov 5, 2013, at 11:27 AM, Rui Pedro Leal <rui.pedro.leal@gmail.com
> <mailto:rui.pedro.leal@gmail.com>> wrote:
>
>> Hello Kevin,
>>
>> That's exactly my immediate (and hopefully wrong) conclusion: the
>> getXXX calls are really costly!
>>
>> So, you are proposing using the binary protocol. I've never known the
>> existence of that. Is this it:
>> http://wiki.postgresql.org/wiki/JDBC-BinaryTransfer ?
>>
>> Thanks.
>>
>> [] Rui Pedro Leal
>> [] rui.pedro.leal@gmail.com <mailto:rui.pedro.leal@gmail.com>
>>
>> On 2013/11/05, at 18:14, Kevin Wooten wrote:
>>
>>> Well I can speak from experience...
>>>
>>> When I was comparing the performance of my implementation with
>>> pgjdbc I was very disheartened at the beginning as pgjdbc was
>>> seemingly much faster. It was only after I switched the test to
>>> take into account the getXXX calls that the true cost of using the
>>> driver was discovered. If you just execute a query and ignore the
>>> results pgjdbc is essentially just receiving a stream of text. It
>>> ignores all of the work of turning that text into Java
>>> objects/primitives.
>>>
>>> My implementation uses binary protocol for this reason and that
>>> removes a lot of the overhead that pgjdbc has. If pgAdmin is using
>>> the binary protocol as well this could very well be the difference
>>> that is being seen, because it is a significant amount of time.
>>>
>>> On Nov 5, 2013, at 11:03 AM, Dave Cramer <pg@fastcrypt.com
>>> <mailto:pg@fastcrypt.com>> wrote:
>>>
>>>> I do not know, the best way to figure this out is to use explain
>>>> analyze in pg_admin.
>>>>
>>>> What I am debating is the java code is using a cursor. pgAdmin is not.
>>>>
>>>> Try it without setFetchSize()
>>>>
>>>> Dave Cramer
>>>>
>>>> dave.cramer(at)credativ(dot)ca
>>>> http://www.credativ.ca <http://www.credativ.ca/;
>>>>
>>>>
>>>> On Tue, Nov 5, 2013 at 1:00 PM, Rui Pedro Leal
>>>> <rui.pedro.leal@gmail.com <mailto:rui.pedro.leal@gmail.com>> wrote:
>>>>
>>>> Dave,
>>>>
>>>> I understand what you are saying, but i must get the results,
>>>> right? :)
>>>>
>>>> I think in pgAdmin the total query time, presented by the query
>>>> execution windows, is also considering the rendering /
>>>> presenting of the query results, so this is comparable ... right?
>>>>
>>>> [] Rui Pedro Leal
>>>> [] rui.pedro.leal@gmail.com <mailto:rui.pedro.leal@gmail.com>
>>>>
>>>> On 2013/11/05, at 17:34, Dave Cramer wrote:
>>>>
>>>>> Rui Leal,
>>>>>
>>>>> You shouldn't see that much of a penatly, but I question how
>>>>> you are measuring it. Since you are timing the loop which is
>>>>> using cursors.
>>>>>
>>>>> Just run a simple query and time it without iterating through
>>>>> the result set. That would be equivalent
>>>>>
>>>>> Dave Cramer
>>>>>
>>>>> dave.cramer(at)credativ(dot)ca
>>>>> http://www.credativ.ca <http://www.credativ.ca/;
>>>>>
>>>>>
>>>>> On Tue, Nov 5, 2013 at 12:16 PM, Rui Pedro Leal
>>>>> <rui.pedro.leal@gmail.com <mailto:rui.pedro.leal@gmail.com>>
>>>>> wrote:
>>>>>
>>>>> Hello Dave, thanks for the quick reply.
>>>>>
>>>>> I'm just executing the same query on pgAdmin query
>>>>> interface. Just a way to measure performance of the query
>>>>> + getting results vs the JDBC way.
>>>>>
>>>>> I'm using fetchsize in order to get some faster results
>>>>> when iterating the resultSet. I've tried not to use
>>>>> fetchSize/cursor but the performance is naturally slower
>>>>> (and worser than the simple pgAdmin query).
>>>>>
>>>>> What i'm asking is .. is this performance penalty i'm
>>>>> getting using postgres JDBC normal and within what's
>>>>> expected? I'm i doing something wrong? Can i do it in any
>>>>> other way?
>>>>>
>>>>> Regards,
>>>>>
>>>>> Rui Leal
>>>>>
>>>>> [] Rui Pedro Leal
>>>>> [] rui.pedro.leal@gmail.com <mailto:rui.pedro.leal@gmail.com>
>>>>>
>>>>>
>>>>> On 2013/11/05, at 16:43, Dave Cramer wrote:
>>>>>
>>>>>> How can you measure the same query in pgAdmin ? JDBC will
>>>>>> use a cursor when you set the fetchsize.
>>>>>>
>>>>>> Dave Cramer
>>>>>>
>>>>>> dave.cramer(at)credativ(dot)ca
>>>>>> http://www.credativ.ca <http://www.credativ.ca/;
>>>>>>
>>>>>>
>>>>>> On Tue, Nov 5, 2013 at 11:12 AM, Rui Pedro Leal
>>>>>> <rui.pedro.leal@gmail.com
>>>>>> <mailto:rui.pedro.leal@gmail.com>> wrote:
>>>>>>
>>>>>> Just an quick info update: i'm using the
>>>>>> postgresql-9.2-1003-jdbc4 driver
>>>>>>
>>>>>> On 2013/11/05, at 16:09, Rui Pedro Leal wrote:
>>>>>>
>>>>>> > Hello everyone,
>>>>>> >
>>>>>> > I'm having slow JDBC performance on iterating a
>>>>>> resultSet obtained from a somewhat simple query.
>>>>>> >
>>>>>> > The query, although using PostGIS functions, is
>>>>>> pretty straight forward:
>>>>>> >
>>>>>> > SELECT _id, _spatial_type, ST_AsBinary(_geometry),
>>>>>> _attribute, _count, _references, countpersons,
>>>>>> countfatals, countdrunks, density
>>>>>> > FROM accidents_5
>>>>>> > WHERE (_geometry &&
>>>>>> ST_MakeEnvelope(-126.60644531250001,26.43122806450644,-63.369140625,52.96187505907603,
>>>>>> 4326) )
>>>>>> > ORDER BY _pk_id
>>>>>> >
>>>>>> > The results are the following:
>>>>>> > - Executing the query directly from pgAdmin: ~2807ms
>>>>>> > - Executing from JVM + JDBC: 4184ms
>>>>>> >
>>>>>> > The code i'm executing is pretty much standard:
>>>>>> >
>>>>>> > -------------------- code --------------------
>>>>>> >
>>>>>> > public static void main(final String[] args) throws
>>>>>> Exception {
>>>>>> >
>>>>>> > String sql = "SELECT _id, _spatial_type,
>>>>>> ST_AsBinary(_geometry), _attribute, _count,
>>>>>> _references, countpersons, countfatals, countdrunks,
>>>>>> density";
>>>>>> > sql += " FROM accidents_5";
>>>>>> > sql += " WHERE (_geometry &&
>>>>>> ST_MakeEnvelope(-126.60644531250001,26.43122806450644,-63.369140625,52.96187505907603,
>>>>>> 4326) )";
>>>>>> > sql += " ORDER BY _pk_id";
>>>>>> >
>>>>>> > System.out.println(sql);
>>>>>> >
>>>>>> > Long time = System.currentTimeMillis();
>>>>>> > try {
>>>>>> > Connection connection =
>>>>>> DataStoreInfo.getDataStores().get(0);
>>>>>> > connection.setAutoCommit(false);
>>>>>> >
>>>>>> > System.out.println("[QUERY " +
>>>>>> Thread.currentThread().getId()
>>>>>> > + "] - connection in "
>>>>>> > + (System.currentTimeMillis() - time) + "ms.");
>>>>>> >
>>>>>> > Statement st = connection.createStatement(
>>>>>> > ResultSet.TYPE_FORWARD_ONLY,
>>>>>> ResultSet.CONCUR_READ_ONLY,
>>>>>> > ResultSet.CLOSE_CURSORS_AT_COMMIT);
>>>>>> > st.setFetchSize(250);
>>>>>> >
>>>>>> > System.out.println("[QUERY " +
>>>>>> Thread.currentThread().getId()
>>>>>> > + "] - statement in " + (System.currentTimeMillis()
>>>>>> - time)
>>>>>> > + "ms.");
>>>>>> >
>>>>>> > ResultSet resultSet = st.executeQuery(sql);
>>>>>> >
>>>>>> > System.out.println("[QUERY " +
>>>>>> Thread.currentThread().getId()
>>>>>> > + "] - done in " + (System.currentTimeMillis() - time)
>>>>>> > + "ms.");
>>>>>> >
>>>>>> > GeometryFactory geofact = new GeometryFactory(new
>>>>>> PrecisionModel(),4326);
>>>>>> > WKBReader wkbReader = new WKBReader(geofact);
>>>>>> >
>>>>>> > String id;
>>>>>> > String spatialType;
>>>>>> > Geometry geometry;
>>>>>> > String attribute;
>>>>>> > Long count;
>>>>>> > String reference;
>>>>>> > Map<String, Double> properties;
>>>>>> > String granularSynthString = "GranularSynthesis";
>>>>>> >
>>>>>> > while (resultSet.next()) {
>>>>>> > id = resultSet.getString(1);
>>>>>> > spatialType = resultSet.getString(2);
>>>>>> >
>>>>>> > // geometry =
>>>>>> wkbReader.read(resultSet.getBytes(3)); // ignored the
>>>>>> WKBReader and the results are about the same.
>>>>>> > attribute = resultSet.getString(4);
>>>>>> > count = resultSet.getLong(5);
>>>>>> > reference = resultSet.getString(6);
>>>>>> >
>>>>>> > properties = new HashMap<String, Double>();
>>>>>> > Double aux = resultSet.getDouble(7);
>>>>>> > properties.put("countpersons", aux);
>>>>>> > aux = resultSet.getDouble(8);
>>>>>> > properties.put("countfatals", aux);
>>>>>> > aux = resultSet.getDouble(9);
>>>>>> > properties.put("countdrunks", aux);
>>>>>> > aux = resultSet.getDouble(10);
>>>>>> > properties.put("density", aux);
>>>>>> > }
>>>>>> >
>>>>>> > System.out.println("[QUERY " +
>>>>>> Thread.currentThread().getId()
>>>>>> > + "] - done & iterated in "
>>>>>> > + (System.currentTimeMillis() - time) + "ms.");
>>>>>> >
>>>>>> > resultSet.close();
>>>>>> > st.close();
>>>>>> > connection.commit();
>>>>>> > connection.close();
>>>>>> > } catch (SQLException exception) {
>>>>>> > exception.printStackTrace();
>>>>>> > // } catch (ParseException exception) { //
>>>>>> ignored from WKBreader
>>>>>> > // exception.printStackTrace();
>>>>>> > }
>>>>>> >
>>>>>> > System.out.println("[End " +
>>>>>> Thread.currentThread().getId()
>>>>>> > + "] - done in " +
>>>>>> (System.currentTimeMillis() - time) + "ms.");
>>>>>> > }
>>>>>> >
>>>>>> > -------------------- end code --------------------
>>>>>> >
>>>>>> > Although i'm executing this on slow MacbookPro (2.6
>>>>>> core duo, 2Gbs RAM but SSD) and have a 9.1.2
>>>>>> postgres, i've also tested this on a recent retina
>>>>>> MBP and the ratio between pgAdmin and JDBC execution
>>>>>> is similar.
>>>>>> >
>>>>>> > Is this expected? Can someone point if i'm doing
>>>>>> something terrible wrong?
>>>>>> >
>>>>>> > I'm not concerned about the query performance
>>>>>> per-se (i know it CAN be optimized), but the
>>>>>> differences just using JDBC and iterating the
>>>>>> resultSet are really annoying.
>>>>>> >
>>>>>> > Thanks in advance for any help.
>>>>>> >
>>>>>> > Kind regards,
>>>>>> >
>>>>>> > Rui Leal
>>>>>> >
>>>>>> >
>>>>>>
>>>>>>
>>>>>>
>>>>>> --
>>>>>> Sent via pgsql-jdbc mailing list
>>>>>> (pgsql-jdbc@postgresql.org
>>>>>> <mailto:pgsql-jdbc@postgresql.org>)
>>>>>> To make changes to your subscription:
>>>>>> http://www.postgresql.org/mailpref/pgsql-jdbc
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>
>>
>
Does this patches already integrated into main stream driver source code?
Edson
^ permalink raw reply [nested|flat] 29+ messages in thread
* Re: JDBC slow performance on resultSet iteration?
@ 2013-11-05 18:55 Kevin Wooten <kdubb@me.com>
parent: Edson Richter <edsonrichter@hotmail.com>
0 siblings, 1 reply; 29+ messages in thread
From: Kevin Wooten @ 2013-11-05 18:55 UTC (permalink / raw)
To: Edson Richter <edsonrichter@hotmail.com>; +Cc: pgsql-jdbc
I believe it, or some version of it, is.
On Nov 5, 2013, at 11:50 AM, Edson Richter <edsonrichter@hotmail.com> wrote:
> Em 05/11/2013 16:45, Kevin Wooten escreveu:
>> Well yes and no. The driver may be transferring your data in binary already but we should work out if that is actually true. I believe that option "-Dorg.postgresql.forcebinary=boolean” still works and forces an Extended Query which will allow binary transfers.
>>
>> Try running with that option and see if it changes the timings.
>>
>> On Nov 5, 2013, at 11:27 AM, Rui Pedro Leal <rui.pedro.leal@gmail.com> wrote:
>>
>>> Hello Kevin,
>>>
>>> That's exactly my immediate (and hopefully wrong) conclusion: the getXXX calls are really costly!
>>>
>>> So, you are proposing using the binary protocol. I've never known the existence of that. Is this it: http://wiki.postgresql.org/wiki/JDBC-BinaryTransfer ?
>>>
>>> Thanks.
>>>
>>> [] Rui Pedro Leal
>>> [] rui.pedro.leal@gmail.com
>>>
>>> On 2013/11/05, at 18:14, Kevin Wooten wrote:
>>>
>>>> Well I can speak from experience…
>>>>
>>>> When I was comparing the performance of my implementation with pgjdbc I was very disheartened at the beginning as pgjdbc was seemingly much faster. It was only after I switched the test to take into account the getXXX calls that the true cost of using the driver was discovered. If you just execute a query and ignore the results pgjdbc is essentially just receiving a stream of text. It ignores all of the work of turning that text into Java objects/primitives.
>>>>
>>>> My implementation uses binary protocol for this reason and that removes a lot of the overhead that pgjdbc has. If pgAdmin is using the binary protocol as well this could very well be the difference that is being seen, because it is a significant amount of time.
>>>>
>>>> On Nov 5, 2013, at 11:03 AM, Dave Cramer <pg@fastcrypt.com> wrote:
>>>>
>>>>> I do not know, the best way to figure this out is to use explain analyze in pg_admin.
>>>>>
>>>>> What I am debating is the java code is using a cursor. pgAdmin is not.
>>>>>
>>>>> Try it without setFetchSize()
>>>>>
>>>>> Dave Cramer
>>>>>
>>>>> dave.cramer(at)credativ(dot)ca
>>>>> http://www.credativ.ca
>>>>>
>>>>>
>>>>> On Tue, Nov 5, 2013 at 1:00 PM, Rui Pedro Leal <rui.pedro.leal@gmail.com> wrote:
>>>>> Dave,
>>>>>
>>>>> I understand what you are saying, but i must get the results, right? :)
>>>>>
>>>>> I think in pgAdmin the total query time, presented by the query execution windows, is also considering the rendering / presenting of the query results, so this is comparable ... right?
>>>>>
>>>>> [] Rui Pedro Leal
>>>>> [] rui.pedro.leal@gmail.com
>>>>>
>>>>> On 2013/11/05, at 17:34, Dave Cramer wrote:
>>>>>
>>>>>> Rui Leal,
>>>>>>
>>>>>> You shouldn't see that much of a penatly, but I question how you are measuring it. Since you are timing the loop which is using cursors.
>>>>>>
>>>>>> Just run a simple query and time it without iterating through the result set. That would be equivalent
>>>>>>
>>>>>> Dave Cramer
>>>>>>
>>>>>> dave.cramer(at)credativ(dot)ca
>>>>>> http://www.credativ.ca
>>>>>>
>>>>>>
>>>>>> On Tue, Nov 5, 2013 at 12:16 PM, Rui Pedro Leal <rui.pedro.leal@gmail.com> wrote:
>>>>>> Hello Dave, thanks for the quick reply.
>>>>>>
>>>>>> I'm just executing the same query on pgAdmin query interface. Just a way to measure performance of the query + getting results vs the JDBC way.
>>>>>>
>>>>>> I'm using fetchsize in order to get some faster results when iterating the resultSet. I've tried not to use fetchSize/cursor but the performance is naturally slower (and worser than the simple pgAdmin query).
>>>>>>
>>>>>> What i'm asking is .. is this performance penalty i'm getting using postgres JDBC normal and within what's expected? I'm i doing something wrong? Can i do it in any other way?
>>>>>>
>>>>>> Regards,
>>>>>>
>>>>>> Rui Leal
>>>>>>
>>>>>> [] Rui Pedro Leal
>>>>>> [] rui.pedro.leal@gmail.com
>>>>>>
>>>>>>
>>>>>> On 2013/11/05, at 16:43, Dave Cramer wrote:
>>>>>>
>>>>>>> How can you measure the same query in pgAdmin ? JDBC will use a cursor when you set the fetchsize.
>>>>>>>
>>>>>>> Dave Cramer
>>>>>>>
>>>>>>> dave.cramer(at)credativ(dot)ca
>>>>>>> http://www.credativ.ca
>>>>>>>
>>>>>>>
>>>>>>> On Tue, Nov 5, 2013 at 11:12 AM, Rui Pedro Leal <rui.pedro.leal@gmail.com> wrote:
>>>>>>> Just an quick info update: i'm using the postgresql-9.2-1003-jdbc4 driver
>>>>>>>
>>>>>>> On 2013/11/05, at 16:09, Rui Pedro Leal wrote:
>>>>>>>
>>>>>>> > Hello everyone,
>>>>>>> >
>>>>>>> > I'm having slow JDBC performance on iterating a resultSet obtained from a somewhat simple query.
>>>>>>> >
>>>>>>> > The query, although using PostGIS functions, is pretty straight forward:
>>>>>>> >
>>>>>>> > SELECT _id, _spatial_type, ST_AsBinary(_geometry), _attribute, _count, _references, countpersons, countfatals, countdrunks, density
>>>>>>> > FROM accidents_5
>>>>>>> > WHERE (_geometry && ST_MakeEnvelope(-126.60644531250001,26.43122806450644,-63.369140625,52.96187505907603, 4326) )
>>>>>>> > ORDER BY _pk_id
>>>>>>> >
>>>>>>> > The results are the following:
>>>>>>> > - Executing the query directly from pgAdmin: ~2807ms
>>>>>>> > - Executing from JVM + JDBC: 4184ms
>>>>>>> >
>>>>>>> > The code i'm executing is pretty much standard:
>>>>>>> >
>>>>>>> > -------------------- code --------------------
>>>>>>> >
>>>>>>> > public static void main(final String[] args) throws Exception {
>>>>>>> >
>>>>>>> > String sql = "SELECT _id, _spatial_type, ST_AsBinary(_geometry), _attribute, _count, _references, countpersons, countfatals, countdrunks, density";
>>>>>>> > sql += " FROM accidents_5";
>>>>>>> > sql += " WHERE (_geometry && ST_MakeEnvelope(-126.60644531250001,26.43122806450644,-63.369140625,52.96187505907603, 4326) )";
>>>>>>> > sql += " ORDER BY _pk_id";
>>>>>>> >
>>>>>>> > System.out.println(sql);
>>>>>>> >
>>>>>>> > Long time = System.currentTimeMillis();
>>>>>>> > try {
>>>>>>> > Connection connection = DataStoreInfo.getDataStores().get(0);
>>>>>>> > connection.setAutoCommit(false);
>>>>>>> >
>>>>>>> > System.out.println("[QUERY " + Thread.currentThread().getId()
>>>>>>> > + "] - connection in "
>>>>>>> > + (System.currentTimeMillis() - time) + "ms.");
>>>>>>> >
>>>>>>> > Statement st = connection.createStatement(
>>>>>>> > ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY,
>>>>>>> > ResultSet.CLOSE_CURSORS_AT_COMMIT);
>>>>>>> > st.setFetchSize(250);
>>>>>>> >
>>>>>>> > System.out.println("[QUERY " + Thread.currentThread().getId()
>>>>>>> > + "] - statement in " + (System.currentTimeMillis() - time)
>>>>>>> > + "ms.");
>>>>>>> >
>>>>>>> > ResultSet resultSet = st.executeQuery(sql);
>>>>>>> >
>>>>>>> > System.out.println("[QUERY " + Thread.currentThread().getId()
>>>>>>> > + "] - done in " + (System.currentTimeMillis() - time)
>>>>>>> > + "ms.");
>>>>>>> >
>>>>>>> > GeometryFactory geofact = new GeometryFactory(new PrecisionModel(),4326);
>>>>>>> > WKBReader wkbReader = new WKBReader(geofact);
>>>>>>> >
>>>>>>> > String id;
>>>>>>> > String spatialType;
>>>>>>> > Geometry geometry;
>>>>>>> > String attribute;
>>>>>>> > Long count;
>>>>>>> > String reference;
>>>>>>> > Map<String, Double> properties;
>>>>>>> > String granularSynthString = "GranularSynthesis";
>>>>>>> >
>>>>>>> > while (resultSet.next()) {
>>>>>>> > id = resultSet.getString(1);
>>>>>>> > spatialType = resultSet.getString(2);
>>>>>>> >
>>>>>>> > // geometry = wkbReader.read(resultSet.getBytes(3)); // ignored the WKBReader and the results are about the same.
>>>>>>> > attribute = resultSet.getString(4);
>>>>>>> > count = resultSet.getLong(5);
>>>>>>> > reference = resultSet.getString(6);
>>>>>>> >
>>>>>>> > properties = new HashMap<String, Double>();
>>>>>>> > Double aux = resultSet.getDouble(7);
>>>>>>> > properties.put("countpersons", aux);
>>>>>>> > aux = resultSet.getDouble(8);
>>>>>>> > properties.put("countfatals", aux);
>>>>>>> > aux = resultSet.getDouble(9);
>>>>>>> > properties.put("countdrunks", aux);
>>>>>>> > aux = resultSet.getDouble(10);
>>>>>>> > properties.put("density", aux);
>>>>>>> > }
>>>>>>> >
>>>>>>> > System.out.println("[QUERY " + Thread.currentThread().getId()
>>>>>>> > + "] - done & iterated in "
>>>>>>> > + (System.currentTimeMillis() - time) + "ms.");
>>>>>>> >
>>>>>>> > resultSet.close();
>>>>>>> > st.close();
>>>>>>> > connection.commit();
>>>>>>> > connection.close();
>>>>>>> > } catch (SQLException exception) {
>>>>>>> > exception.printStackTrace();
>>>>>>> > // } catch (ParseException exception) { // ignored from WKBreader
>>>>>>> > // exception.printStackTrace();
>>>>>>> > }
>>>>>>> >
>>>>>>> > System.out.println("[End " + Thread.currentThread().getId()
>>>>>>> > + "] - done in " + (System.currentTimeMillis() - time) + "ms.");
>>>>>>> > }
>>>>>>> >
>>>>>>> > -------------------- end code --------------------
>>>>>>> >
>>>>>>> > Although i'm executing this on slow MacbookPro (2.6 core duo, 2Gbs RAM but SSD) and have a 9.1.2 postgres, i've also tested this on a recent retina MBP and the ratio between pgAdmin and JDBC execution is similar.
>>>>>>> >
>>>>>>> > Is this expected? Can someone point if i'm doing something terrible wrong?
>>>>>>> >
>>>>>>> > I'm not concerned about the query performance per-se (i know it CAN be optimized), but the differences just using JDBC and iterating the resultSet are really annoying.
>>>>>>> >
>>>>>>> > Thanks in advance for any help.
>>>>>>> >
>>>>>>> > Kind regards,
>>>>>>> >
>>>>>>> > Rui Leal
>>>>>>> >
>>>>>>> >
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> --
>>>>>>> Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
>>>>>>> To make changes to your subscription:
>>>>>>> http://www.postgresql.org/mailpref/pgsql-jdbc
>>>>>>>
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>
>>>
>>
> Does this patches already integrated into main stream driver source code?
>
> Edson
^ permalink raw reply [nested|flat] 29+ messages in thread
* Re: JDBC slow performance on resultSet iteration?
@ 2013-11-05 19:31 Glenn Holmer <gholmer@weycogroup.com>
parent: Kevin Wooten <kdubb@me.com>
0 siblings, 1 reply; 29+ messages in thread
From: Glenn Holmer @ 2013-11-05 19:31 UTC (permalink / raw)
To: pgsql-jdbc
On 11/05/2013 12:55 PM, Kevin Wooten wrote:
> I believe it, or some version of it, is.
Interesting thread, but good god! would you guys trim your posts?
--
Glenn Holmer
Weyco Group, Inc.
phone: 414-908-1809
fax: 414-908-1601
--
Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-jdbc
^ permalink raw reply [nested|flat] 29+ messages in thread
* Re: JDBC slow performance on resultSet iteration?
@ 2013-11-05 20:00 Edson Richter <edsonrichter@hotmail.com>
parent: Glenn Holmer <gholmer@weycogroup.com>
0 siblings, 0 replies; 29+ messages in thread
From: Edson Richter @ 2013-11-05 20:00 UTC (permalink / raw)
To: pgsql-jdbc
Em 05/11/2013 17:31, Glenn Holmer escreveu:
> On 11/05/2013 12:55 PM, Kevin Wooten wrote:
>> I believe it, or some version of it, is.
>
> Interesting thread, but good god! would you guys trim your posts?
>
I used to answer as in PostgreSQL list... "do not top post, keep the
thread" :-)
--
Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-jdbc
^ permalink raw reply [nested|flat] 29+ messages in thread
* Re: JDBC slow performance on resultSet iteration?
@ 2013-11-06 03:46 Rui Pedro Leal <rui.pedro.leal@gmail.com>
parent: Kevin Wooten <kdubb@me.com>
1 sibling, 1 reply; 29+ messages in thread
From: Rui Pedro Leal @ 2013-11-06 03:46 UTC (permalink / raw)
To: Kevin Wooten <kdubb@me.com>; +Cc: pgsql-jdbc
Hi Kevin,
I've tested that "-Dorg.postgresql.forcebinary=true” options with no apparent benefits.
Just recapping ...
Using postgres 9.1.2 + postgis .... with the 9.2-1003-jdbc4 driver.
For the sake of having consistent result, I'm now just focused now on running a createStatement / executeQuery with no cursor or fetchSize stuff (as per Dave suggestion).
As i've presented in my previous code, i'm measuring the process from the beginning to the end (always accumulating time) and measuring connection creation, statement creation (i was using preparedStatement previously), query execution and result set iteration + resultSet.getXXX.
So currently, i'm getting the following results (last execution after arriving at home):
[QUERY 12] - connection in 8ms.
[QUERY 12] - statement in 8ms.
[QUERY 12] - done/execution in 3035ms.
[QUERY 12] - iterated in 4381ms.
If i recall correctly, since i'm not using fetchSize all results should be loaded to memory, if there is space (around 190k of them).
Comparing to pgAdmin, where executing the query and presenting all the results is around ~2663ms (last execution after arriving at home).
So, my conclusions so far ... to which i welcome feedback or pointing out my utterly stupidity ;)
1) The performance difference on the execution is for me quite expected, not that abnormal and almost negligible (from my previous experience with JDBC on Oracle and MySQL).
2) However, the step of the iteration/gets on the resultSet (190k) .... which is mostly all resultSet.getXXX (please check my original code / post) is an additional 1.3s. If the query results are in fact in memory, it is strange and puzzling to me.
In the end, i'm getting an additional 1.7s performance hit vs. a simple query and return results of pgAdmin.
Tomorrow i will follow Dave Cramer suggestion and turn on log_duration on the server and check in more detail the server logs. Will report back.
Any additional feedback or suggestion?
Thanks everyone for the promptly feedback ...
Regards,
Rui Leal
[] Rui Pedro Leal
[] rui.pedro.leal@gmail.com
On 2013/11/05, at 18:45, Kevin Wooten wrote:
> Well yes and no. The driver may be transferring your data in binary already but we should work out if that is actually true. I believe that option "-Dorg.postgresql.forcebinary=boolean” still works and forces an Extended Query which will allow binary transfers.
>
> Try running with that option and see if it changes the timings.
>
> On Nov 5, 2013, at 11:27 AM, Rui Pedro Leal <rui.pedro.leal@gmail.com> wrote:
>
>> Hello Kevin,
>>
>> That's exactly my immediate (and hopefully wrong) conclusion: the getXXX calls are really costly!
>>
>> So, you are proposing using the binary protocol. I've never known the existence of that. Is this it: http://wiki.postgresql.org/wiki/JDBC-BinaryTransfer ?
>>
>> Thanks.
>>
>> [] Rui Pedro Leal
>> [] rui.pedro.leal@gmail.com
>>
>> On 2013/11/05, at 18:14, Kevin Wooten wrote:
>>
>>> Well I can speak from experience…
>>>
>>> When I was comparing the performance of my implementation with pgjdbc I was very disheartened at the beginning as pgjdbc was seemingly much faster. It was only after I switched the test to take into account the getXXX calls that the true cost of using the driver was discovered. If you just execute a query and ignore the results pgjdbc is essentially just receiving a stream of text. It ignores all of the work of turning that text into Java objects/primitives.
>>>
>>> My implementation uses binary protocol for this reason and that removes a lot of the overhead that pgjdbc has. If pgAdmin is using the binary protocol as well this could very well be the difference that is being seen, because it is a significant amount of time.
>>>
>>> On Nov 5, 2013, at 11:03 AM, Dave Cramer <pg@fastcrypt.com> wrote:
>>>
>>>> I do not know, the best way to figure this out is to use explain analyze in pg_admin.
>>>>
>>>> What I am debating is the java code is using a cursor. pgAdmin is not.
>>>>
>>>> Try it without setFetchSize()
>>>>
>>>> Dave Cramer
>>>>
>>>> dave.cramer(at)credativ(dot)ca
>>>> http://www.credativ.ca
>>>>
>>>>
>>>> On Tue, Nov 5, 2013 at 1:00 PM, Rui Pedro Leal <rui.pedro.leal@gmail.com> wrote:
>>>> Dave,
>>>>
>>>> I understand what you are saying, but i must get the results, right? :)
>>>>
>>>> I think in pgAdmin the total query time, presented by the query execution windows, is also considering the rendering / presenting of the query results, so this is comparable ... right?
>>>>
>>>> [] Rui Pedro Leal
>>>> [] rui.pedro.leal@gmail.com
>>>>
>>>> On 2013/11/05, at 17:34, Dave Cramer wrote:
>>>>
>>>>> Rui Leal,
>>>>>
>>>>> You shouldn't see that much of a penatly, but I question how you are measuring it. Since you are timing the loop which is using cursors.
>>>>>
>>>>> Just run a simple query and time it without iterating through the result set. That would be equivalent
>>>>>
>>>>> Dave Cramer
>>>>>
>>>>> dave.cramer(at)credativ(dot)ca
>>>>> http://www.credativ.ca
>>>>>
>>>>>
>>>>> On Tue, Nov 5, 2013 at 12:16 PM, Rui Pedro Leal <rui.pedro.leal@gmail.com> wrote:
>>>>> Hello Dave, thanks for the quick reply.
>>>>>
>>>>> I'm just executing the same query on pgAdmin query interface. Just a way to measure performance of the query + getting results vs the JDBC way.
>>>>>
>>>>> I'm using fetchsize in order to get some faster results when iterating the resultSet. I've tried not to use fetchSize/cursor but the performance is naturally slower (and worser than the simple pgAdmin query).
>>>>>
>>>>> What i'm asking is .. is this performance penalty i'm getting using postgres JDBC normal and within what's expected? I'm i doing something wrong? Can i do it in any other way?
>>>>>
>>>>> Regards,
>>>>>
>>>>> Rui Leal
>>>>>
>>>>> [] Rui Pedro Leal
>>>>> [] rui.pedro.leal@gmail.com
>>>>>
>>>>>
>>>>> On 2013/11/05, at 16:43, Dave Cramer wrote:
>>>>>
>>>>>> How can you measure the same query in pgAdmin ? JDBC will use a cursor when you set the fetchsize.
>>>>>>
>>>>>> Dave Cramer
>>>>>>
>>>>>> dave.cramer(at)credativ(dot)ca
>>>>>> http://www.credativ.ca
>>>>>>
>>>>>>
>>>>>> On Tue, Nov 5, 2013 at 11:12 AM, Rui Pedro Leal <rui.pedro.leal@gmail.com> wrote:
>>>>>> Just an quick info update: i'm using the postgresql-9.2-1003-jdbc4 driver
>>>>>>
>>>>>> On 2013/11/05, at 16:09, Rui Pedro Leal wrote:
>>>>>>
>>>>>> > Hello everyone,
>>>>>> >
>>>>>> > I'm having slow JDBC performance on iterating a resultSet obtained from a somewhat simple query.
>>>>>> >
>>>>>> > The query, although using PostGIS functions, is pretty straight forward:
>>>>>> >
>>>>>> > SELECT _id, _spatial_type, ST_AsBinary(_geometry), _attribute, _count, _references, countpersons, countfatals, countdrunks, density
>>>>>> > FROM accidents_5
>>>>>> > WHERE (_geometry && ST_MakeEnvelope(-126.60644531250001,26.43122806450644,-63.369140625,52.96187505907603, 4326) )
>>>>>> > ORDER BY _pk_id
>>>>>> >
>>>>>> > The results are the following:
>>>>>> > - Executing the query directly from pgAdmin: ~2807ms
>>>>>> > - Executing from JVM + JDBC: 4184ms
>>>>>> >
>>>>>> > The code i'm executing is pretty much standard:
>>>>>> >
>>>>>> > -------------------- code --------------------
>>>>>> >
>>>>>> > public static void main(final String[] args) throws Exception {
>>>>>> >
>>>>>> > String sql = "SELECT _id, _spatial_type, ST_AsBinary(_geometry), _attribute, _count, _references, countpersons, countfatals, countdrunks, density";
>>>>>> > sql += " FROM accidents_5";
>>>>>> > sql += " WHERE (_geometry && ST_MakeEnvelope(-126.60644531250001,26.43122806450644,-63.369140625,52.96187505907603, 4326) )";
>>>>>> > sql += " ORDER BY _pk_id";
>>>>>> >
>>>>>> > System.out.println(sql);
>>>>>> >
>>>>>> > Long time = System.currentTimeMillis();
>>>>>> > try {
>>>>>> > Connection connection = DataStoreInfo.getDataStores().get(0);
>>>>>> > connection.setAutoCommit(false);
>>>>>> >
>>>>>> > System.out.println("[QUERY " + Thread.currentThread().getId()
>>>>>> > + "] - connection in "
>>>>>> > + (System.currentTimeMillis() - time) + "ms.");
>>>>>> >
>>>>>> > Statement st = connection.createStatement(
>>>>>> > ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY,
>>>>>> > ResultSet.CLOSE_CURSORS_AT_COMMIT);
>>>>>> > st.setFetchSize(250);
>>>>>> >
>>>>>> > System.out.println("[QUERY " + Thread.currentThread().getId()
>>>>>> > + "] - statement in " + (System.currentTimeMillis() - time)
>>>>>> > + "ms.");
>>>>>> >
>>>>>> > ResultSet resultSet = st.executeQuery(sql);
>>>>>> >
>>>>>> > System.out.println("[QUERY " + Thread.currentThread().getId()
>>>>>> > + "] - done in " + (System.currentTimeMillis() - time)
>>>>>> > + "ms.");
>>>>>> >
>>>>>> > GeometryFactory geofact = new GeometryFactory(new PrecisionModel(),4326);
>>>>>> > WKBReader wkbReader = new WKBReader(geofact);
>>>>>> >
>>>>>> > String id;
>>>>>> > String spatialType;
>>>>>> > Geometry geometry;
>>>>>> > String attribute;
>>>>>> > Long count;
>>>>>> > String reference;
>>>>>> > Map<String, Double> properties;
>>>>>> > String granularSynthString = "GranularSynthesis";
>>>>>> >
>>>>>> > while (resultSet.next()) {
>>>>>> > id = resultSet.getString(1);
>>>>>> > spatialType = resultSet.getString(2);
>>>>>> >
>>>>>> > // geometry = wkbReader.read(resultSet.getBytes(3)); // ignored the WKBReader and the results are about the same.
>>>>>> > attribute = resultSet.getString(4);
>>>>>> > count = resultSet.getLong(5);
>>>>>> > reference = resultSet.getString(6);
>>>>>> >
>>>>>> > properties = new HashMap<String, Double>();
>>>>>> > Double aux = resultSet.getDouble(7);
>>>>>> > properties.put("countpersons", aux);
>>>>>> > aux = resultSet.getDouble(8);
>>>>>> > properties.put("countfatals", aux);
>>>>>> > aux = resultSet.getDouble(9);
>>>>>> > properties.put("countdrunks", aux);
>>>>>> > aux = resultSet.getDouble(10);
>>>>>> > properties.put("density", aux);
>>>>>> > }
>>>>>> >
>>>>>> > System.out.println("[QUERY " + Thread.currentThread().getId()
>>>>>> > + "] - done & iterated in "
>>>>>> > + (System.currentTimeMillis() - time) + "ms.");
>>>>>> >
>>>>>> > resultSet.close();
>>>>>> > st.close();
>>>>>> > connection.commit();
>>>>>> > connection.close();
>>>>>> > } catch (SQLException exception) {
>>>>>> > exception.printStackTrace();
>>>>>> > // } catch (ParseException exception) { // ignored from WKBreader
>>>>>> > // exception.printStackTrace();
>>>>>> > }
>>>>>> >
>>>>>> > System.out.println("[End " + Thread.currentThread().getId()
>>>>>> > + "] - done in " + (System.currentTimeMillis() - time) + "ms.");
>>>>>> > }
>>>>>> >
>>>>>> > -------------------- end code --------------------
>>>>>> >
>>>>>> > Although i'm executing this on slow MacbookPro (2.6 core duo, 2Gbs RAM but SSD) and have a 9.1.2 postgres, i've also tested this on a recent retina MBP and the ratio between pgAdmin and JDBC execution is similar.
>>>>>> >
>>>>>> > Is this expected? Can someone point if i'm doing something terrible wrong?
>>>>>> >
>>>>>> > I'm not concerned about the query performance per-se (i know it CAN be optimized), but the differences just using JDBC and iterating the resultSet are really annoying.
>>>>>> >
>>>>>> > Thanks in advance for any help.
>>>>>> >
>>>>>> > Kind regards,
>>>>>> >
>>>>>> > Rui Leal
>>>>>> >
>>>>>> >
>>>>>>
>>>>>>
>>>>>>
>>>>>> --
>>>>>> Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
>>>>>> To make changes to your subscription:
>>>>>> http://www.postgresql.org/mailpref/pgsql-jdbc
>>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>
>>
>
^ permalink raw reply [nested|flat] 29+ messages in thread
* Re: JDBC slow performance on resultSet iteration?
@ 2013-11-08 02:47 Edson Richter <edsonrichter@hotmail.com>
parent: Rui Pedro Leal <rui.pedro.leal@gmail.com>
0 siblings, 0 replies; 29+ messages in thread
From: Edson Richter @ 2013-11-08 02:47 UTC (permalink / raw)
To: pgsql-jdbc
Hi, Rui,
I'm very interested in knowing your results.
Please, share your advances with us...
Regards,
Edson
Em 06/11/2013 01:46, Rui Pedro Leal escreveu:
> Hi Kevin,
>
> I've tested that "-Dorg.postgresql.forcebinary=true" options with no
> apparent benefits.
>
> Just recapping ...
>
> Using postgres 9.1.2 + postgis .... with the 9.2-1003-jdbc4 driver.
>
> For the sake of having consistent result, I'm now just focused now on
> running a createStatement / executeQuery with no cursor or fetchSize
> stuff (as per Dave suggestion).
>
> As i've presented in my previous code, i'm measuring the process from
> the beginning to the end (always accumulating time) and measuring
> connection creation, statement creation (i was using preparedStatement
> previously), query execution and result set iteration + resultSet.getXXX.
>
> So currently, i'm getting the following results (last execution after
> arriving at home):
>
> [QUERY 12] - connection in 8ms.
> [QUERY 12] - statement in 8ms.
> [QUERY 12] - done/execution in 3035ms.
> [QUERY 12] - iterated in 4381ms.
>
> If i recall correctly, since i'm not using fetchSize all results
> should be loaded to memory, if there is space (around 190k of them).
>
> Comparing to pgAdmin, where executing the query and presenting all the
> results is around ~2663ms (last execution after arriving at home).
>
> So, my conclusions so far ... to which i welcome feedback or pointing
> out my utterly stupidity ;)
>
> 1) The performance difference on the execution is for me quite
> expected, not that abnormal and almost negligible (from my previous
> experience with JDBC on Oracle and MySQL).
> 2) However, the step of the iteration/gets on the resultSet (190k)
> .... which is mostly all resultSet.getXXX (please check my original
> code / post) is an additional 1.3s. If the query results are in fact
> in memory, it is strange and puzzling to me.
>
> In the end, i'm getting an additional 1.7s performance hit vs. a
> simple query and return results of pgAdmin.
>
> Tomorrow i will follow Dave Cramer suggestion and turn on log_duration
> on the server and check in more detail the server logs. Will report back.
>
> Any additional feedback or suggestion?
>
> Thanks everyone for the promptly feedback ...
>
> Regards,
>
> Rui Leal
>
> [] Rui Pedro Leal
> [] rui.pedro.leal@gmail.com <mailto:rui.pedro.leal@gmail.com>
>
> On 2013/11/05, at 18:45, Kevin Wooten wrote:
>
>> Well yes and no. The driver may be transferring your data in binary
>> already but we should work out if that is actually true. I believe
>> that option "-Dorg.postgresql.forcebinary=boolean" still works and
>> forces an Extended Query which will allow binary transfers.
>>
>> Try running with that option and see if it changes the timings.
>>
>> On Nov 5, 2013, at 11:27 AM, Rui Pedro Leal <rui.pedro.leal@gmail.com
>> <mailto:rui.pedro.leal@gmail.com>> wrote:
>>
>>> Hello Kevin,
>>>
>>> That's exactly my immediate (and hopefully wrong) conclusion: the
>>> getXXX calls are really costly!
>>>
>>> So, you are proposing using the binary protocol. I've never known
>>> the existence of that. Is this it:
>>> http://wiki.postgresql.org/wiki/JDBC-BinaryTransfer ?
>>>
>>> Thanks.
>>>
>>> [] Rui Pedro Leal
>>> [] rui.pedro.leal@gmail.com <mailto:rui.pedro.leal@gmail.com>
>>>
>>> On 2013/11/05, at 18:14, Kevin Wooten wrote:
>>>
>>>> Well I can speak from experience...
>>>>
>>>> When I was comparing the performance of my implementation with
>>>> pgjdbc I was very disheartened at the beginning as pgjdbc was
>>>> seemingly much faster. It was only after I switched the test to
>>>> take into account the getXXX calls that the true cost of using the
>>>> driver was discovered. If you just execute a query and ignore the
>>>> results pgjdbc is essentially just receiving a stream of text. It
>>>> ignores all of the work of turning that text into Java
>>>> objects/primitives.
>>>>
>>>> My implementation uses binary protocol for this reason and that
>>>> removes a lot of the overhead that pgjdbc has. If pgAdmin is using
>>>> the binary protocol as well this could very well be the difference
>>>> that is being seen, because it is a significant amount of time.
>>>>
>>>> On Nov 5, 2013, at 11:03 AM, Dave Cramer <pg@fastcrypt.com
>>>> <mailto:pg@fastcrypt.com>> wrote:
>>>>
>>>>> I do not know, the best way to figure this out is to use explain
>>>>> analyze in pg_admin.
>>>>>
>>>>> What I am debating is the java code is using a cursor. pgAdmin is not.
>>>>>
>>>>> Try it without setFetchSize()
>>>>>
>>>>> Dave Cramer
>>>>>
>>>>> dave.cramer(at)credativ(dot)ca
>>>>> http://www.credativ.ca <http://www.credativ.ca/;
>>>>>
>>>>>
>>>>> On Tue, Nov 5, 2013 at 1:00 PM, Rui Pedro Leal
>>>>> <rui.pedro.leal@gmail.com <mailto:rui.pedro.leal@gmail.com>> wrote:
>>>>>
>>>>> Dave,
>>>>>
>>>>> I understand what you are saying, but i must get the results,
>>>>> right? :)
>>>>>
>>>>> I think in pgAdmin the total query time, presented by the
>>>>> query execution windows, is also considering the rendering /
>>>>> presenting of the query results, so this is comparable ... right?
>>>>>
>>>>> [] Rui Pedro Leal
>>>>> [] rui.pedro.leal@gmail.com <mailto:rui.pedro.leal@gmail.com>
>>>>>
>>>>> On 2013/11/05, at 17:34, Dave Cramer wrote:
>>>>>
>>>>>> Rui Leal,
>>>>>>
>>>>>> You shouldn't see that much of a penatly, but I question how
>>>>>> you are measuring it. Since you are timing the loop which is
>>>>>> using cursors.
>>>>>>
>>>>>> Just run a simple query and time it without iterating through
>>>>>> the result set. That would be equivalent
>>>>>>
>>>>>> Dave Cramer
>>>>>>
>>>>>> dave.cramer(at)credativ(dot)ca
>>>>>> http://www.credativ.ca <http://www.credativ.ca/;
>>>>>>
>>>>>>
>>>>>> On Tue, Nov 5, 2013 at 12:16 PM, Rui Pedro Leal
>>>>>> <rui.pedro.leal@gmail.com <mailto:rui.pedro.leal@gmail.com>>
>>>>>> wrote:
>>>>>>
>>>>>> Hello Dave, thanks for the quick reply.
>>>>>>
>>>>>> I'm just executing the same query on pgAdmin query
>>>>>> interface. Just a way to measure performance of the query
>>>>>> + getting results vs the JDBC way.
>>>>>>
>>>>>> I'm using fetchsize in order to get some faster results
>>>>>> when iterating the resultSet. I've tried not to use
>>>>>> fetchSize/cursor but the performance is naturally slower
>>>>>> (and worser than the simple pgAdmin query).
>>>>>>
>>>>>> What i'm asking is .. is this performance penalty i'm
>>>>>> getting using postgres JDBC normal and within what's
>>>>>> expected? I'm i doing something wrong? Can i do it in any
>>>>>> other way?
>>>>>>
>>>>>> Regards,
>>>>>>
>>>>>> Rui Leal
>>>>>>
>>>>>> [] Rui Pedro Leal
>>>>>> [] rui.pedro.leal@gmail.com <mailto:rui.pedro.leal@gmail.com>
>>>>>>
>>>>>>
>>>>>> On 2013/11/05, at 16:43, Dave Cramer wrote:
>>>>>>
>>>>>>> How can you measure the same query in pgAdmin ? JDBC
>>>>>>> will use a cursor when you set the fetchsize.
>>>>>>>
>>>>>>> Dave Cramer
>>>>>>>
>>>>>>> dave.cramer(at)credativ(dot)ca
>>>>>>> http://www.credativ.ca <http://www.credativ.ca/;
>>>>>>>
>>>>>>>
>>>>>>> On Tue, Nov 5, 2013 at 11:12 AM, Rui Pedro Leal
>>>>>>> <rui.pedro.leal@gmail.com
>>>>>>> <mailto:rui.pedro.leal@gmail.com>> wrote:
>>>>>>>
>>>>>>> Just an quick info update: i'm using the
>>>>>>> postgresql-9.2-1003-jdbc4 driver
>>>>>>>
>>>>>>> On 2013/11/05, at 16:09, Rui Pedro Leal wrote:
>>>>>>>
>>>>>>> > Hello everyone,
>>>>>>> >
>>>>>>> > I'm having slow JDBC performance on iterating a
>>>>>>> resultSet obtained from a somewhat simple query.
>>>>>>> >
>>>>>>> > The query, although using PostGIS functions, is
>>>>>>> pretty straight forward:
>>>>>>> >
>>>>>>> > SELECT _id, _spatial_type, ST_AsBinary(_geometry),
>>>>>>> _attribute, _count, _references, countpersons,
>>>>>>> countfatals, countdrunks, density
>>>>>>> > FROM accidents_5
>>>>>>> > WHERE (_geometry &&
>>>>>>> ST_MakeEnvelope(-126.60644531250001,26.43122806450644,-63.369140625,52.96187505907603,
>>>>>>> 4326) )
>>>>>>> > ORDER BY _pk_id
>>>>>>> >
>>>>>>> > The results are the following:
>>>>>>> > - Executing the query directly from pgAdmin: ~2807ms
>>>>>>> > - Executing from JVM + JDBC: 4184ms
>>>>>>> >
>>>>>>> > The code i'm executing is pretty much standard:
>>>>>>> >
>>>>>>> > -------------------- code --------------------
>>>>>>> >
>>>>>>> > public static void main(final String[] args)
>>>>>>> throws Exception {
>>>>>>> >
>>>>>>> > String sql = "SELECT _id, _spatial_type,
>>>>>>> ST_AsBinary(_geometry), _attribute, _count,
>>>>>>> _references, countpersons, countfatals, countdrunks,
>>>>>>> density";
>>>>>>> > sql += " FROM accidents_5";
>>>>>>> > sql += " WHERE (_geometry &&
>>>>>>> ST_MakeEnvelope(-126.60644531250001,26.43122806450644,-63.369140625,52.96187505907603,
>>>>>>> 4326) )";
>>>>>>> > sql += " ORDER BY _pk_id";
>>>>>>> >
>>>>>>> > System.out.println(sql);
>>>>>>> >
>>>>>>> > Long time = System.currentTimeMillis();
>>>>>>> > try {
>>>>>>> > Connection connection =
>>>>>>> DataStoreInfo.getDataStores().get(0);
>>>>>>> > connection.setAutoCommit(false);
>>>>>>> >
>>>>>>> > System.out.println("[QUERY " +
>>>>>>> Thread.currentThread().getId()
>>>>>>> > + "] - connection in "
>>>>>>> > + (System.currentTimeMillis() - time) + "ms.");
>>>>>>> >
>>>>>>> > Statement st = connection.createStatement(
>>>>>>> > ResultSet.TYPE_FORWARD_ONLY,
>>>>>>> ResultSet.CONCUR_READ_ONLY,
>>>>>>> > ResultSet.CLOSE_CURSORS_AT_COMMIT);
>>>>>>> > st.setFetchSize(250);
>>>>>>> >
>>>>>>> > System.out.println("[QUERY " +
>>>>>>> Thread.currentThread().getId()
>>>>>>> > + "] - statement in " +
>>>>>>> (System.currentTimeMillis() - time)
>>>>>>> > + "ms.");
>>>>>>> >
>>>>>>> > ResultSet resultSet = st.executeQuery(sql);
>>>>>>> >
>>>>>>> > System.out.println("[QUERY " +
>>>>>>> Thread.currentThread().getId()
>>>>>>> > + "] - done in " + (System.currentTimeMillis() - time)
>>>>>>> > + "ms.");
>>>>>>> >
>>>>>>> > GeometryFactory geofact = new GeometryFactory(new
>>>>>>> PrecisionModel(),4326);
>>>>>>> > WKBReader wkbReader = new WKBReader(geofact);
>>>>>>> >
>>>>>>> > String id;
>>>>>>> > String spatialType;
>>>>>>> > Geometry geometry;
>>>>>>> > String attribute;
>>>>>>> > Long count;
>>>>>>> > String reference;
>>>>>>> > Map<String, Double> properties;
>>>>>>> > String granularSynthString = "GranularSynthesis";
>>>>>>> >
>>>>>>> > while (resultSet.next()) {
>>>>>>> > id = resultSet.getString(1);
>>>>>>> > spatialType = resultSet.getString(2);
>>>>>>> >
>>>>>>> > // geometry =
>>>>>>> wkbReader.read(resultSet.getBytes(3)); // ignored
>>>>>>> the WKBReader and the results are about the same.
>>>>>>> > attribute = resultSet.getString(4);
>>>>>>> > count = resultSet.getLong(5);
>>>>>>> > reference = resultSet.getString(6);
>>>>>>> >
>>>>>>> > properties = new HashMap<String, Double>();
>>>>>>> > Double aux = resultSet.getDouble(7);
>>>>>>> > properties.put("countpersons", aux);
>>>>>>> > aux = resultSet.getDouble(8);
>>>>>>> > properties.put("countfatals", aux);
>>>>>>> > aux = resultSet.getDouble(9);
>>>>>>> > properties.put("countdrunks", aux);
>>>>>>> > aux = resultSet.getDouble(10);
>>>>>>> > properties.put("density", aux);
>>>>>>> > }
>>>>>>> >
>>>>>>> > System.out.println("[QUERY " +
>>>>>>> Thread.currentThread().getId()
>>>>>>> > + "] - done & iterated in "
>>>>>>> > + (System.currentTimeMillis() - time) + "ms.");
>>>>>>> >
>>>>>>> > resultSet.close();
>>>>>>> > st.close();
>>>>>>> > connection.commit();
>>>>>>> > connection.close();
>>>>>>> > } catch (SQLException exception) {
>>>>>>> > exception.printStackTrace();
>>>>>>> > // } catch (ParseException exception) { //
>>>>>>> ignored from WKBreader
>>>>>>> > // exception.printStackTrace();
>>>>>>> > }
>>>>>>> >
>>>>>>> > System.out.println("[End " +
>>>>>>> Thread.currentThread().getId()
>>>>>>> > + "] - done in " +
>>>>>>> (System.currentTimeMillis() - time) + "ms.");
>>>>>>> > }
>>>>>>> >
>>>>>>> > -------------------- end code --------------------
>>>>>>> >
>>>>>>> > Although i'm executing this on slow MacbookPro
>>>>>>> (2.6 core duo, 2Gbs RAM but SSD) and have a 9.1.2
>>>>>>> postgres, i've also tested this on a recent retina
>>>>>>> MBP and the ratio between pgAdmin and JDBC execution
>>>>>>> is similar.
>>>>>>> >
>>>>>>> > Is this expected? Can someone point if i'm doing
>>>>>>> something terrible wrong?
>>>>>>> >
>>>>>>> > I'm not concerned about the query performance
>>>>>>> per-se (i know it CAN be optimized), but the
>>>>>>> differences just using JDBC and iterating the
>>>>>>> resultSet are really annoying.
>>>>>>> >
>>>>>>> > Thanks in advance for any help.
>>>>>>> >
>>>>>>> > Kind regards,
>>>>>>> >
>>>>>>> > Rui Leal
>>>>>>> >
>>>>>>> >
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> --
>>>>>>> Sent via pgsql-jdbc mailing list
>>>>>>> (pgsql-jdbc@postgresql.org
>>>>>>> <mailto:pgsql-jdbc@postgresql.org>)
>>>>>>> To make changes to your subscription:
>>>>>>> http://www.postgresql.org/mailpref/pgsql-jdbc
>>>>>>>
>>>>>>>
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>
>>>
>>
>
^ permalink raw reply [nested|flat] 29+ messages in thread
end of thread, other threads:[~2013-11-08 02:47 UTC | newest]
Thread overview: 29+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2013-11-05 16:09 JDBC slow performance on resultSet iteration? Rui Pedro Leal <rui.pedro.leal@gmail.com>
2013-11-05 16:12 ` Rui Pedro Leal <rui.pedro.leal@gmail.com>
2013-11-05 16:43 ` Dave Cramer <pg@fastcrypt.com>
2013-11-05 17:16 ` Rui Pedro Leal <rui.pedro.leal@gmail.com>
2013-11-05 17:34 ` Dave Cramer <pg@fastcrypt.com>
2013-11-05 17:39 ` Kevin Wooten <kdubb@me.com>
2013-11-05 17:42 ` Dave Cramer <pg@fastcrypt.com>
2013-11-05 17:47 ` Edson Richter <edsonrichter@hotmail.com>
2013-11-05 18:05 ` Rui Pedro Leal <rui.pedro.leal@gmail.com>
2013-11-05 18:08 ` Dave Cramer <pg@fastcrypt.com>
2013-11-05 18:23 ` Kevin Wooten <kdubb@me.com>
2013-11-05 18:25 ` Dave Cramer <pg@fastcrypt.com>
2013-11-05 18:34 ` Kevin Wooten <kdubb@me.com>
2013-11-05 18:14 ` Edson Richter <edsonrichter@hotmail.com>
2013-11-05 18:34 ` Rui Pedro Leal <rui.pedro.leal@gmail.com>
2013-11-05 18:02 ` Rui Pedro Leal <rui.pedro.leal@gmail.com>
2013-11-05 18:00 ` Rui Pedro Leal <rui.pedro.leal@gmail.com>
2013-11-05 18:03 ` Dave Cramer <pg@fastcrypt.com>
2013-11-05 18:11 ` Rui Pedro Leal <rui.pedro.leal@gmail.com>
2013-11-05 18:16 ` Dave Cramer <pg@fastcrypt.com>
2013-11-05 18:14 ` Kevin Wooten <kdubb@me.com>
2013-11-05 18:27 ` Rui Pedro Leal <rui.pedro.leal@gmail.com>
2013-11-05 18:45 ` Kevin Wooten <kdubb@me.com>
2013-11-05 18:50 ` Edson Richter <edsonrichter@hotmail.com>
2013-11-05 18:55 ` Kevin Wooten <kdubb@me.com>
2013-11-05 19:31 ` Glenn Holmer <gholmer@weycogroup.com>
2013-11-05 20:00 ` Edson Richter <edsonrichter@hotmail.com>
2013-11-06 03:46 ` Rui Pedro Leal <rui.pedro.leal@gmail.com>
2013-11-08 02:47 ` Edson Richter <edsonrichter@hotmail.com>
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