X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 096DD5284F for ; Wed, 6 Jul 2005 18:29:45 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 87165-02 for ; Wed, 6 Jul 2005 21:29:35 +0000 (GMT) Received: from sccmmhc92.asp.att.net (sccmmhc92.asp.att.net [204.127.203.212]) by svr1.postgresql.org (Postfix) with ESMTP id 8245E5285C for ; Wed, 6 Jul 2005 18:29:34 -0300 (ADT) Received: from juju.arbash-meinel.com ([12.214.18.81]) by sccmmhc92.asp.att.net (sccmmhc92) with ESMTP id <20050706212920m920060ieqe>; Wed, 6 Jul 2005 21:29:35 +0000 Received: by juju.arbash-meinel.com (Postfix, from userid 505) id 286DF56086; Wed, 6 Jul 2005 16:29:19 -0500 (CDT) Received: from [129.255.168.114] (host168-114.uihc.uiowa.edu [129.255.168.114]) by juju.arbash-meinel.com (Postfix) with ESMTP id E62835607F; Wed, 6 Jul 2005 16:29:07 -0500 (CDT) Message-ID: <42CC4D1C.9040606@arbash-meinel.com> Date: Wed, 06 Jul 2005 16:29:00 -0500 From: John A Meinel User-Agent: Mozilla Thunderbird 1.0.2 (Macintosh/20050317) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Niccolo Rigacci , Postgresql Performance Subject: Re: CURSOR slowes down a WHERE clause 100 times? References: <20050706211946.GA3460@rigacci.org> In-Reply-To: <20050706211946.GA3460@rigacci.org> X-Enigmail-Version: 0.91.0.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="------------enig0BDC10F2B83E07547A560931" X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.025 tagged_above=0 required=5 tests=AWL, FORGED_RCVD_HELO X-Spam-Level: X-Archive-Number: 200507/53 X-Sequence-Number: 13294 This is an OpenPGP/MIME signed message (RFC 2440 and 3156) --------------enig0BDC10F2B83E07547A560931 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Niccolo Rigacci wrote: >Hi to all, > >I have a performace problem with the following query: > > BEGIN; > DECLARE mycursor BINARY CURSOR FOR > SELECT > toponimo, > wpt > FROM wpt_comuni_view > WHERE ( > wpt && > setSRID('BOX3D(4.83 36, 20.16 47.5)'::BOX3D, 4326) > ); > FETCH ALL IN mycursor; > END; > >I get the results in about 108 seconds (8060 rows). > >If I issue the SELECT alone (without the CURSOR) I get the >same results in less than 1 second. > >The wpt_comuni_view is a VIEW of a 3 tables JOIN, and the "wpt" >field is a PostGIS geometry column. The "&&" is the PostGIS >"overlaps" operator. > >If I CURSOR SELECT from a temp table instead of the JOIN VIEW the >query time 1 second. > >If I omit the WHERE clause the CURSOR fetches results in 1 >second. > >Can the CURSOR on JOIN affects so heavly the WHERE clause? I >suspect that - with the CURSOR - a sequential scan is performed >on the entire data set for each fetched record... > >Any idea? > > What does it say if you do "EXPLAIN ANALYZE SELECT..." both with and without the cursor? It may not say much for the cursor, but I think you can explain analyze the fetch statements. It is my understanding that Cursors generally favor using an slow-startup style plan, which usually means using an index, because it expects that you won't actually want all of the data. A seqscan is not always slower, especially if you need to go through most of the data. Without an explain analyze it's hard to say what the planner is thinking and doing. >This is the definition of the VIEW: > > CREATE VIEW wpt_comuni_view AS > SELECT istat_wpt.oid, istat_wpt.id, istat_wpt.toponimo, > istat_comuni.residenti, istat_wpt.wpt > FROM istat_comuni > JOIN istat_comuni2wpt > USING (idprovincia, idcomune) > JOIN istat_wpt > ON (idwpt = id); > >Thank you for any hint. > > > You might also try comparing your CURSOR to a prepared statement. There are a few rare cases where preparing is worse than issuing the query directly, depending on your data layout. John =:-> --------------enig0BDC10F2B83E07547A560931 Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.0 (Darwin) Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org iD8DBQFCzE0fJdeBCYSNAAMRAk+eAJ96Sz+yQMX94j1ba77ojk021iA6IQCfYRvm ynbwpZw6yU9j96fu225RJvA= =DWez -----END PGP SIGNATURE----- --------------enig0BDC10F2B83E07547A560931--