pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Laurenz Albe <laurenz.albe@cybertec.at>
To: Klaudie Willis <Klaudie.Willis@protonmail.com>
To: PostgreSQL General <pgsql-general@lists.postgresql.org>
Subject: Re: Bitmap scan seem like such a strange choice when "limit 1"
Date: Fri, 04 Sep 2020 14:14:15 +0200
Message-ID: <a3d7ed35f09992174c968ff809e89ecc7fbb95f9.camel@cybertec.at> (raw)
In-Reply-To: <X5rHjtT8gkMBFiaErcYyqD2R3r887Zxd9s15EutKCF2DxNxrxjm_JNp4YDyF-ANfXOr-xf-6PKoY5Lfq0X468FGrTJIh_KUaM4pXw8sum1s=@protonmail.com>
References: <X5rHjtT8gkMBFiaErcYyqD2R3r887Zxd9s15EutKCF2DxNxrxjm_JNp4YDyF-ANfXOr-xf-6PKoY5Lfq0X468FGrTJIh_KUaM4pXw8sum1s=@protonmail.com>

On Fri, 2020-09-04 at 11:42 +0000, Klaudie Willis wrote:
> Postgres 13 beta3
> 
> set enable_bitmapscan=1; -- default
> explain (analyze,buffers) 
> select *
> from bigtable 
> where cars_ref = 1769854207 and t > '2020-01-01'::timestamp  and  t < '2021-01-01'::timestamp 
> limit 1
> 
> Short story.  Big table > 100M rows. b-tree index on cars_ref, the t constraints limits it to one partition, but I don't think that is very relevant.  In any case, running this query takes 1.5s:
> 
> Limit  (cost=23728.33..23729.24 rows=1 width=635) (actual time=1516.865..1516.867 rows=1 loops=1)
>   Buffers: shared hit=2376
>   ->  Bitmap Heap Scan on bigtable_y2020 bigtable (cost=23728.33..2530109.01 rows=2730872 width=635) (actual time=1516.863..1516.864 rows=1 loops=1)
>         Recheck Cond: (cars_ref = 1769854207)
>         Filter: ((t > '2020-01-01 00:00:00'::timestamp without time zone) AND (t < '2021-01-01 00:00:00'::timestamp without time zone))
>         Heap Blocks: exact=1
>         Buffers: shared hit=2376
>         ->  Bitmap Index Scan on bigtable_y2020_cars_ref_idx  (cost=0.00..23045.61 rows=2731965 width=0) (actual time=751.640..751.640 rows=2817675 loops=1)
>               Index Cond: (cars_ref = 1769854207)
>               Buffers: shared hit=2375
> Planning Time: 0.365 ms
> Execution Time: 1540.207 ms
> 
> 1.5 seconds seems a lot for a single indexed row.  I would think it should be instant, and so it is when I disable bitmap scan with: set enable_bitmapscan=0
> 
> Limit  (cost=0.57..1.60 rows=1 width=636) (actual time=0.027..0.028 rows=1 loops=1)
>   Buffers: shared hit=5
>   ->  Index Scan using bigtable_y2020_cars_ref_idx on bigtable_y2020 bigtable (cost=0.57..2966738.51 rows=2873818 width=636) (actual time=0.026..0.026 rows=1 loops=1)
>         Index Cond: (cars_ref = 1769854207)
>         Filter: ((t > '2020-01-01 00:00:00'::timestamp without time zone) AND (t < '2021-01-01 00:00:00'::timestamp without time zone))
>         Buffers: shared hit=5
> Planning Time: 0.291 ms
> Execution Time: 0.049 ms
> 
> But I am not supposed to disable bitmap scan!  So why on earth do Postgres 13 beta3 think that returning 1 row, should be done with a bitmap scan?
> I noticed that different values for "cars_ref" result in different plans in the query above.  I belive that it has to do with wheter or not the cars_ref is in the "most common value" list.  But in
> any case, I cant see why a bitmap scan is wise then you expect one row.

PostgreSQL estimates that 2817675 rows satisfy the index condition and expects
that it will have to scan many of them before it finds one that satisfies the
filter condition.  That turns out to be a wrong guess.

You could create an index on (cars_ref, t), then PostgreSQL will certainly
pick an index scan.

Yours,
Laurenz Albe
-- 
Cybertec | https://www.cybertec-postgresql.com






view thread (7+ messages)  latest in thread

Message-ID: <a3d7ed35f09992174c968ff809e89ecc7fbb95f9.camel@cybertec.at>
Permalink:  ../a3d7ed35f09992174c968ff809e89ecc7fbb95f9.camel@cybertec.at/
Also on:    postgresql.org/message-id/a3d7ed35f09992174c968ff809e89ecc7fbb95f9.camel@cybertec.at

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: laurenz.albe@cybertec.at, Klaudie.Willis@protonmail.com
  Subject: Re: Bitmap scan seem like such a strange choice when "limit 1"
  In-Reply-To: <a3d7ed35f09992174c968ff809e89ecc7fbb95f9.camel@cybertec.at>
  References: <X5rHjtT8gkMBFiaErcYyqD2R3r887Zxd9s15EutKCF2DxNxrxjm_JNp4YDyF-ANfXOr-xf-6PKoY5Lfq0X468FGrTJIh_KUaM4pXw8sum1s=@protonmail.com> <a3d7ed35f09992174c968ff809e89ecc7fbb95f9.camel@cybertec.at>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox