X-Original-To: pgsql-performance-postgresql.org@postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id AB2629FA5F0 for ; Tue, 20 Jun 2006 11:28:32 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 44126-08 for ; Tue, 20 Jun 2006 11:28:22 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by postgresql.org (Postfix) with ESMTP id 5781D9FA219 for ; Tue, 20 Jun 2006 11:28:22 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.6/8.13.6) with ESMTP id k5KESI0O027875; Tue, 20 Jun 2006 10:28:18 -0400 (EDT) To: biuro@globeinphotos.com cc: pgsql-performance@postgresql.org Subject: Re: Curson prbolem In-reply-to: <20060620143932.e7u8gr5s53i808ss@gdn.superhost.pl> References: <20060620143932.e7u8gr5s53i808ss@gdn.superhost.pl> Comments: In-reply-to biuro@globeinphotos.com message dated "Tue, 20 Jun 2006 14:39:32 +0200" Date: Tue, 20 Jun 2006 10:28:18 -0400 Message-ID: <27874.1150813698@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200606/386 X-Sequence-Number: 19743 biuro@globeinphotos.com writes: > [slow:] > OPEN cursor1 FOR SELECT * FROM alias WHERE mask>=alias_out > ORDER BY mask; > [fast:] > OPEN cursor1 FOR SELECT * FROM alias WHERE mask>=alias_out > ORDER BY mask LIMIT 100; The difference is that in the first case the planner has to assume you intend to fetch all the rows with mask>=something (and I'll bet the something is a plpgsql variable, so the planner can't even see its value). In this case a sort-based plan looks like a winner. In the second case, since you only need to fetch 100 rows, it's clearly best to scan the index beginning at mask = alias_out. > Can somebody clarify what is wrong with my example? I need select > without LIMIT 100 part. Why? You should always tell the SQL engine what it is that you really want --- leaving it in the dark about your intentions is a good way to destroy performance, as you are finding out. If I were you I would get rid of the row-counting inside the loop entirely, and use the "LIMIT n" clause to handle that. regards, tom lane