Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fQHE8-0007xN-U7 for pgsql-general@arkaria.postgresql.org; Tue, 05 Jun 2018 19:02:41 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fQHE7-0008J1-EW for pgsql-general@arkaria.postgresql.org; Tue, 05 Jun 2018 19:02:39 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fQHE7-0008Ir-3K for pgsql-general@lists.postgresql.org; Tue, 05 Jun 2018 19:02:39 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fQHE0-0005Y8-Bf for pgsql-general@lists.postgresql.org; Tue, 05 Jun 2018 19:02:37 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id w55J2TpL009056; Tue, 5 Jun 2018 15:02:29 -0400 From: Tom Lane To: armand pirvu cc: "pgsql-generallists.postgresql.org" Subject: Re: limit and query planner In-reply-to: <59ED87E7-B582-481A-9FD8-CB77C7A3B268@gmail.com> References: <59ED87E7-B582-481A-9FD8-CB77C7A3B268@gmail.com> Comments: In-reply-to armand pirvu message dated "Tue, 05 Jun 2018 13:24:25 -0500" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <9054.1528225349.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Tue, 05 Jun 2018 15:02:29 -0400 Message-ID: <9055.1528225349@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk armand pirvu writes: > My question is: I suspect the limit simply limits the fethching to the f= irst n-records retrieved and has no implications whatsoever on the planner= , meaning the planner ignores it. Am I right or wrong ? You're quite wrong. The presence of a LIMIT causes the planner to prefer "fast start" plans, since it will then optimize on the basis of picking the lowest estimated cost to fetch the first N rows. As an example, you're more likely to get an ordered indexscan than a seqscan-and-sort for small N, though there are many cases where seqscan-and-sort wins if the need is to fetch the whole table. regards, tom lane