Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VdjCm-0001om-Kd for pgsql-jdbc@arkaria.postgresql.org; Tue, 05 Nov 2013 16:10:13 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1VdjCl-0002WX-W1 for pgsql-jdbc@arkaria.postgresql.org; Tue, 05 Nov 2013 16:10:12 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VdjCj-0002Vp-TI for pgsql-jdbc@postgresql.org; Tue, 05 Nov 2013 16:10:10 +0000 Received: from mail-wi0-x22b.google.com ([2a00:1450:400c:c05::22b]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VdjCf-0006dg-4c for pgsql-jdbc@postgresql.org; Tue, 05 Nov 2013 16:10:09 +0000 Received: by mail-wi0-f171.google.com with SMTP id f4so2348023wiw.16 for ; Tue, 05 Nov 2013 08:10:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=subject:from:content-type:message-id:date:to :content-transfer-encoding:mime-version; bh=RmZXvKOp7rPdyH8N54jwqHc4WSRGUFMUtSaMNthlKfc=; b=YW8a8ZtcfTj8G5gaRTM6D3cwhr4vKHTeLGRWJPY2twg2GPkSpUu/mzLrx2O6QCp3xM 7VyNIKhP9hCdnZDOxFUqd7YmqxfABhcwMIzhOChhBfF8BHSxtZIKq7idJGxwbZOo+szF TNHiRQiP7ZaOJVyJ+pJr5rM2abYExq7S+TSLTdSksR12a8z07k9SHPDfEsbPAQhAAR9u NJwlcK+4XsQC0Ho9ZY2E7oEJsvtkVoLBaqYF2iNEI0VRF+WPn2z9kIqIU5OHc/dH6nYK GnufPdYbFmuoNHJjpfWgXiLAGHOnmmfdWe6199loAKhBnSaz523wSWguQXGfwhFp9AKH ZE4g== X-Received: by 10.180.92.10 with SMTP id ci10mr17538922wib.41.1383667803709; Tue, 05 Nov 2013 08:10:03 -0800 (PST) Received: from 10-170-133-246.docente.di.fct.unl.pt (di17.di.fct.unl.pt. [193.136.122.17]) by mx.google.com with ESMTPSA id td6sm15567180wic.10.2013.11.05.08.10.01 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 05 Nov 2013 08:10:02 -0800 (PST) Subject: JDBC slow performance on resultSet iteration? From: Rui Pedro Leal Content-Type: text/plain; charset=us-ascii Message-Id: <73027CA6-419A-4EE4-BA8E-BD02C8802F5D@gmail.com> Date: Tue, 5 Nov 2013 16:09:00 +0000 To: pgsql-jdbc@postgresql.org Content-Transfer-Encoding: quoted-printable Mime-Version: 1.0 (Apple Message framework v1085) X-Mailer: Apple Mail (2.1085) X-Pg-Spam-Score: -0.1 (/) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-jdbc Precedence: bulk Sender: pgsql-jdbc-owner@postgresql.org Hello everyone,=20 I'm having slow JDBC performance on iterating a resultSet obtained from a s= omewhat simple query. The query, although using PostGIS functions, is pretty straight forward: SELECT _id, _spatial_type, ST_AsBinary(_geometry), _attribute, _count, _ref= erences, 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:=20 -------------------- code -------------------- public static void main(final String[] args) throws Exception { String sql =3D "SELECT _id, _spatial_type, ST_AsBinary(_geometry), _attri= bute, _count, _references, countpersons, countfatals, countdrunks, density"; sql +=3D " FROM accidents_5"; sql +=3D " WHERE (_geometry && ST_MakeEnvelope(-126.60644531250001,26.431= 22806450644,-63.369140625,52.96187505907603, 4326) )"; sql +=3D " ORDER BY _pk_id"; System.out.println(sql); Long time =3D System.currentTimeMillis(); try { Connection connection =3D DataStoreInfo.getDataStores().get(0); connection.setAutoCommit(false); System.out.println("[QUERY " + Thread.currentThread().getId() + "] - connection in " + (System.currentTimeMillis() - time) + "ms."); Statement st =3D 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 =3D st.executeQuery(sql); System.out.println("[QUERY " + Thread.currentThread().getId() + "] - done in " + (System.currentTimeMillis() - time) + "ms."); GeometryFactory geofact =3D new GeometryFactory(new PrecisionModel(),432= 6); WKBReader wkbReader =3D new WKBReader(geofact); String id; String spatialType; Geometry geometry; String attribute; Long count; String reference; Map properties; String granularSynthString =3D "GranularSynthesis"; while (resultSet.next()) { id =3D resultSet.getString(1); spatialType =3D resultSet.getString(2); // geometry =3D wkbReader.read(resultSet.getBytes(3)); // ignored the W= KBReader and the results are about the same. attribute =3D resultSet.getString(4); count =3D resultSet.getLong(5); reference =3D resultSet.getString(6); properties =3D new HashMap(); Double aux =3D resultSet.getDouble(7); properties.put("countpersons", aux); aux =3D resultSet.getDouble(8); properties.put("countfatals", aux); aux =3D resultSet.getDouble(9); properties.put("countdrunks", aux); aux =3D 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 MB= P and the ratio between pgAdmin and JDBC execution is similar. Is this expected? Can someone point if i'm doing something terrible wrong?= =20 I'm not concerned about the query performance per-se (i know it CAN be opti= mized), but the differences just using JDBC and iterating the resultSet are= really annoying. Thanks in advance for any help. Kind regards,=20 Rui Leal --=20 Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-jdbc