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 4B6BF52986 for ; Thu, 7 Jul 2005 06:15:35 -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 51953-05 for ; Thu, 7 Jul 2005 09:15:30 +0000 (GMT) Received: from mail.metronet.co.uk (mail.metronet.co.uk [213.162.97.75]) by svr1.postgresql.org (Postfix) with ESMTP id 96E5C52930 for ; Thu, 7 Jul 2005 06:15:21 -0300 (ADT) Received: from mainbox.archonet.com (84-51-143-99.archon037.adsl.metronet.co.uk [84.51.143.99]) by smtp.metronet.co.uk (MetroNet Mail) with ESMTP id 2B7A040EF94; Thu, 7 Jul 2005 10:15:16 +0100 (BST) Received: from localhost (localhost.localdomain [127.0.0.1]) by mainbox.archonet.com (Postfix) with ESMTP id ADAD915ED6; Thu, 7 Jul 2005 10:14:53 +0100 (BST) Received: from mainbox.archonet.com ([127.0.0.1]) by localhost (mainbox [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 20598-09; Thu, 7 Jul 2005 10:14:50 +0100 (BST) Received: from [192.168.1.17] (client17.office.archonet.com [192.168.1.17]) by mainbox.archonet.com (Postfix) with ESMTP id 9232E15ED5; Thu, 7 Jul 2005 10:14:50 +0100 (BST) Message-ID: <42CCF28A.3010403@archonet.com> Date: Thu, 07 Jul 2005 10:14:50 +0100 From: Richard Huxton User-Agent: Mozilla Thunderbird 1.0.2 (X11/20050317) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Niccolo Rigacci Cc: pgsql-performance@postgresql.org Subject: Re: CURSOR slowes down a WHERE clause 100 times? References: <20050706211946.GA3460@rigacci.org> <20050707081327.GA17054@rigacci.org> In-Reply-To: <20050707081327.GA17054@rigacci.org> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.056 tagged_above=0 required=5 tests=AWL, FORGED_RCVD_HELO X-Spam-Level: X-Archive-Number: 200507/60 X-Sequence-Number: 13301 Niccolo Rigacci wrote: >> >>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. > > > By trial and error I discovered that adding an "ORDER BY > toponimo" clause to the SELECT, boosts the CURSOR performances > so that they are now equiparable to the SELECT alone. > > Is there some documentation on how an ORDER can affect the > CURSOR in a different way than the SELECT? I think you're misunderstanding exactly what's happening here. If you ask for a cursor, PG assumes you aren't going to want all the results (or at least not straight away). After all, most people use them to work through results in comparatively small chunks, perhaps only ever fetching 1% of the total results. So - if you ask for a cursor, PG weights things to give you the first few rows as soon as possible, at the expense of fetching *all* rows quickly. If you're only going to fetch e.g. the first 20 rows this is exactly what you want. In your case, since you're immediately issuing FETCH ALL, you're not really using the cursor at all, but PG doesn't know that. So - the ORDER BY means PG has to sort all the results before returning the first row anyway. That probably means the plans with/without cursor are identical. Of course, all this assumes that your configuration settings are good and statistics adequate. To test that, try fetching just the first row from your cursor with/without the ORDER BY. Without should be quicker. -- Richard Huxton Archonet Ltd