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 B9A695E4848 for ; Mon, 6 Sep 2004 17:40:48 +0100 (BST) 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 67157-06 for ; Mon, 6 Sep 2004 16:40:44 +0000 (GMT) Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by svr1.postgresql.org (Postfix) with ESMTP id 735E45E4893 for ; Mon, 6 Sep 2004 17:40:42 +0100 (BST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.12.11/8.12.11) with ESMTP id i86GefrY014847; Mon, 6 Sep 2004 12:40:41 -0400 (EDT) To: =?iso-8859-15?Q?Pierre-Fr=E9d=E9ric_Caillaud?= Cc: pgsql-performance@postgresql.org Subject: Re: The usual sequential scan, but with LIMIT ! In-reply-to: References: <4134BF36.3000504@syscor.com> <413BA104.1040508@geizhals.at> Comments: In-reply-to =?iso-8859-15?Q?Pierre-Fr=E9d=E9ric_Caillaud?= message dated "Mon, 06 Sep 2004 14:15:44 +0200" Date: Mon, 06 Sep 2004 12:40:41 -0400 Message-ID: <14846.1094488841@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200409/40 X-Sequence-Number: 8168 =?iso-8859-15?Q?Pierre-Fr=E9d=E9ric_Caillaud?= writes: > Now, if I LIMIT the query to 10 rows, the index should be used all the > time, because it will always return few rows... well, it doesn't ! Not at all. From the planner's point of view, the LIMIT is going to reduce the cost by about a factor of 10/1403, since the underlying plan step will only be run partway through. That's not going to change the decision about which underlying plan step is cheapest: 10/1403 of a cheaper plan is still always less than 10/1403 of a more expensive plan. Later, you note that LIMIT with ORDER BY does affect the plan choice --- that's because in that situation one plan alternative has a much higher startup cost than the other (namely the cost of a sort step). A small LIMIT can allow the fast-startup plan to be chosen even though it would be estimated to be the loser if run to completion. regards, tom lane