Received: from localhost (unknown [200.46.204.182]) by postgresql.org (Postfix) with ESMTP id 0419C2E0168 for ; Mon, 28 Jan 2008 13:59:25 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.182]) (amavisd-maia, port 10024) with ESMTP id 17906-01 for ; Mon, 28 Jan 2008 13:59:10 -0400 (AST) Received: from smtp111.sbc.mail.mud.yahoo.com (smtp111.sbc.mail.mud.yahoo.com [68.142.198.210]) by postgresql.org (Postfix) with SMTP id E23DD2E011A for ; Mon, 28 Jan 2008 13:59:10 -0400 (AST) Received: (qmail 86862 invoked from network); 28 Jan 2008 17:59:09 -0000 Received: from unknown (HELO zeemac) (pankaj.garg@sbcglobal.net@24.6.87.239 with plain) by smtp111.sbc.mail.mud.yahoo.com with SMTP; 28 Jan 2008 17:59:09 -0000 X-YMail-OSG: SwHM4EAVM1lo1FdyWQUdaCNMtIk67rpa5xGJZuyWV3bgp_7XKrOBc1vMTgjv25LP0B6yB3tcFZvVo_76TLs_xw8_ih5znjmcm5dpqqAq8VZFiz7iL2Y- X-Yahoo-Newman-Property: ymail-3 Message-Id: <018573B2-DC52-4B98-9AB6-1ACB953B0035@zeesource.net> From: Claire McLister To: pgsql-performance@postgresql.org Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes Content-Transfer-Encoding: 7bit Mime-Version: 1.0 (Apple Message framework v915) Subject: JDBC/Stored procedure performance issue Date: Mon, 28 Jan 2008 09:59:07 -0800 X-Mailer: Apple Mail (2.915) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200801/273 X-Sequence-Number: 28512 Hi All, I am experiencing a strange performance issue with Postgresql (7.4.19) + PostGIS. (I posted to the PostGIS list but got no response, so am trying here.) We have a table of entries that contains latitude, longitude values and I have a simple query to retrieve all entries within a specified 2- D box. The latitude, longitude are stored as decimals, plus a trigger stores the corresponding geometry object. When I do an EXPLAIN ANALYZE on one query that returns 3261 rows, it executes in a reasonable 159ms: EXPLAIN ANALYZE SELECT DISTINCT latitude, longitude, color FROM NewEntries WHERE groupid = 57925 AND location @ SetSRID(MakeBox2D(SetSRID(MakePoint(-123.75, 36.597889), 4326), SetSRID(MakePoint(-118.125, 40.979898), 4326)), 4326); Unique (cost=23.73..23.74 rows=1 width=30) (actual time=143.648..156.081 rows=3261 loops=1) -> Sort (cost=23.73..23.73 rows=1 width=30) (actual time=143.640..146.214 rows=3369 loops=1) Sort Key: latitude, longitude, color -> Index Scan using group_index on newentries (cost=0.00..23.72 rows=1 width=30) (actual time=0.184..109.346 rows=3369 loops=1) Index Cond: (groupid = 57925) Filter: ("location" @ '0103000020E610000001000000050000000000000000F05EC0000000A0874C42400000000000F05EC0000000406D7D44400000000000885DC0000000406D7D44400000000000885DC0000000A0874C42400000000000F05EC0000000A0874C4240 '::geometry) Total runtime: 159.430 ms (7 rows) If I issue the same query over JDBC or use a PSQL stored procedure, it takes over 3000 ms, which, of course is unacceptable! Function Scan on gettilelocations (cost=0.00..12.50 rows=1000 width=30) (actual time=3311.368..3319.265 rows=3261 loops=1) Total runtime: 3322.529 ms (2 rows) The function gettilelocations is defined as: CREATE OR REPLACE FUNCTION GetTileLocations(Integer, real, real, real, real) RETURNS SETOF TileLocation AS ' DECLARE R TileLocation; BEGIN FOR R IN SELECT DISTINCT latitude, longitude, color FROM NewEntries WHERE groupid = $1 AND location @ SetSRID(MakeBox2D(SetSRID(MakePoint($2, $3), 4326), SetSRID(MakePoint($4, $5), 4326)), 4326) LOOP RETURN NEXT R; END LOOP; RETURN; END; ' LANGUAGE plpgsql STABLE RETURNS NULL ON NULL INPUT; Can someone please tell me what we are doing wrong? Any help would be greatly appreciated. Thanks Claire -- Claire McLister mclister@zeesource.net 21060 Homestead Road Suite 150 Cupertino, CA 95014 408-733-2737(fax) http://www.zeemaps.com