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 1fQHT1-0000hA-VA for pgsql-general@arkaria.postgresql.org; Tue, 05 Jun 2018 19:18:04 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fQHT0-0004Me-JM for pgsql-general@arkaria.postgresql.org; Tue, 05 Jun 2018 19:18:02 +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 1fQHT0-0004MV-63 for pgsql-general@lists.postgresql.org; Tue, 05 Jun 2018 19:18:02 +0000 Received: from mail-io0-x22a.google.com ([2607:f8b0:4001:c06::22a]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fQHSt-0005tA-Ec for pgsql-general@lists.postgresql.org; Tue, 05 Jun 2018 19:18:00 +0000 Received: by mail-io0-x22a.google.com with SMTP id u4-v6so4723291iof.2 for ; Tue, 05 Jun 2018 12:17:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=QMo2Zj7w4JqehwiSJHCB1fhYIH+vZnBk8zc8CaQDGhU=; b=T2neMxkNm5VNZfd/JW+ogZw22MlucE2kw0u5lPOeYI3ochubS9EJzEcknNo6ZqGJZ0 11XY1tB0TLRYyJyfscdR2TKJJCvesSYxzUAGeILpMhCHX39/ezwx9qeAng2eqsyzuBMd rBmxUfCKcu5FF8dMHr7NadJIa255+Uprefc2NbkTbuSmp5tvO3bVCXYgyTJGEMUTz6tk 3Ceojv7Vn7YIK1eaGhXjvYLajH8ztvoUrXUsGtdgpbbtPs+FjA8nu7LvP3VG8FL4XTfQ ov3QDrq1kGzl2KLgeL3DoPkQhE0+kqAgnYyDGMev/ZOXBt6Eqf0Hs/CjmRfX1EkYsPQ6 KALg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=QMo2Zj7w4JqehwiSJHCB1fhYIH+vZnBk8zc8CaQDGhU=; b=Lq102VyiYkM4ULpGcclAgvQHdYngrgg4FO1FXiee6aj7xNa0kEqn8jqkWyUehXN+dc QUTttMlTVfGV2mE2kC1jykxOtlNufUxO8MkEo2mD7Dah7x1ptCHdVCQLfNnUoDfeLpBI vuU4CaN875n8L2gxajRbmF6s+mKQYwLpehfUe8pyBiRfVECZ4nIl4abX5sCzzzt35mr/ WkejLhpmAoZwfGf72mE60+L3NWfxVjY/roxHKOkL8rX1ZSukM8zJpCLsU5ghOx80e9pd XRTEPZTlKf7umrHRwK5smqHk8HxQrO2ORon3zSqWdHbQaN8Jpfr0J6FKs7EB62GCzigN Utlw== X-Gm-Message-State: APt69E2jE8G3D+Uj9SrE4fNQExAr1Bo4ViFYgIjAkTD3xXcqtrMKoHJh zsozYj9B9UrYcLoupwwsZAQ= X-Google-Smtp-Source: ADUXVKKaU4Kp4wJl2anio8ITKrnkegwPAbBtDcJ6WLTTSJEf/Hra9JRXq0mD2U4D3omV4+rPdXIyJA== X-Received: by 2002:a6b:334f:: with SMTP id z76-v6mr23192267ioz.88.1528226273938; Tue, 05 Jun 2018 12:17:53 -0700 (PDT) Received: from ?IPv6:2605:a601:2062:d00:dc9c:bd1c:7370:376a? ([2605:a601:2062:d00:dc9c:bd1c:7370:376a]) by smtp.gmail.com with ESMTPSA id d202-v6sm1142335itc.30.2018.06.05.12.17.53 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 05 Jun 2018 12:17:53 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 10.3 \(3273\)) Subject: Re: limit and query planner From: armand pirvu In-Reply-To: <9055.1528225349@sss.pgh.pa.us> Date: Tue, 5 Jun 2018 14:17:22 -0500 Cc: "pgsql-generallists.postgresql.org" Content-Transfer-Encoding: quoted-printable Message-Id: <54D22E00-7941-4826-8D91-F4F02DC2EF39@gmail.com> References: <59ED87E7-B582-481A-9FD8-CB77C7A3B268@gmail.com> <9055.1528225349@sss.pgh.pa.us> To: Tom Lane X-Mailer: Apple Mail (2.3273) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk > On Jun 5, 2018, at 2:02 PM, Tom Lane wrote: >=20 > armand pirvu writes: >> My question is: I suspect the limit simply limits the fethching to = the first n-records retrieved and has no implications whatsoever on the = planner, meaning the planner ignores it. Am I right or wrong ? >=20 > 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. >=20 > regards, tom lane Thank you Tom So since=20 select count(*) from sp_i2birst_reg_staging_test; count=20 ------- 6860 and select count(*) from sp_i2birst_reg_staging_test where evt_id =3D = 'ACSF17' =20 and status=3D0 ; count=20 ------- 4239 =20 That means to me I fetch almost the whole table and then I fall in the = case you described seqscan-and-sort wins over indexscan . My statement was made because in the case of an index it gets used as = long as the data returned back falls below 10% (or so) from the total = data in the table and in the case of the original query no matter how = low I get the N still seq scan but I guess is again the above = sescan-and-sort scenario (see below) create index fooidx on sp_i2birst_reg_staging_test (evt_id, status); vacuum analyze sp_i2birst_reg_staging_test; explain analyze select * from sp_i2birst_reg_staging_test where evt_id =3D= 'ACSF17' =20 and status=3D1; QUERY = PLAN =20 Index Scan using fooidx on sp_i2birst_reg_staging_test = (cost=3D0.28..202.91 rows=3D500 width=3D519) (actual time=3D0.097..0.527 = rows=3D500 loops=3D1) Index Cond: (((evt_id)::text =3D 'ACSF17'::text) AND (status =3D 1)) Planning time: 1.024 ms Execution time: 0.766 ms this gets 500 rows out of 6860 explain analyze select * from sp_i2birst_reg_staging_test where evt_id =3D= 'ACSF17' =20 and status=3D1 limit 10; QUERY = PLAN =20= Limit (cost=3D0.28..4.33 rows=3D10 width=3D519) (actual = time=3D0.073..0.105 rows=3D10 loops=3D1) -> Index Scan using fooidx on sp_i2birst_reg_staging_test = (cost=3D0.28..202.91 rows=3D500 width=3D519) (actual time=3D0.072..0.101 = rows=3D10 loops=3D1) Index Cond: (((evt_id)::text =3D 'ACSF17'::text) AND (status =3D = 1)) Planning time: 0.280 ms Execution time: 0.173 ms Back to the original=20 explain analyze select * from sp_i2birst_reg_staging_test where evt_id =3D= 'ACSF17' =20 and status=3D0 limit 1 ; QUERY PLAN = =20 Limit (cost=3D0.00..0.13 rows=3D1 width=3D519) (actual = time=3D0.021..0.021 rows=3D1 loops=3D1) -> Seq Scan on sp_i2birst_reg_staging_test (cost=3D0.00..548.40 = rows=3D4239 width=3D519) (actual time=3D0.019..0.019 rows=3D1 loops=3D1) Filter: (((evt_id)::text =3D 'ACSF17'::text) AND (status =3D = 0)) Rows Removed by Filter: 1 Planning time: 0.286 ms Execution time: 0.110 ms =E2=80=94 Armand=