Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kEAbn-00030j-I6 for pgsql-general@arkaria.postgresql.org; Fri, 04 Sep 2020 12:14:23 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kEAbl-0003aT-Ak for pgsql-general@arkaria.postgresql.org; Fri, 04 Sep 2020 12:14:21 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kEAbl-0003aM-2S for pgsql-general@lists.postgresql.org; Fri, 04 Sep 2020 12:14:21 +0000 Received: from mail-wm1-x344.google.com ([2a00:1450:4864:20::344]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kEAbi-00039S-8b for pgsql-general@lists.postgresql.org; Fri, 04 Sep 2020 12:14:20 +0000 Received: by mail-wm1-x344.google.com with SMTP id c19so7399188wmd.1 for ; Fri, 04 Sep 2020 05:14:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20150623.gappssmtp.com; s=20150623; h=message-id:subject:from:to:date:in-reply-to:references:user-agent :mime-version:content-transfer-encoding; bh=T99iOa1o5CiFTXaeT/jUV6D5RLEpc6JSqxaZqfkYVoU=; b=wJZq1bqeyw+QA/j1TbwtX6aCj1iHDgN76V5DfRiLe2M+1p7Mvp3eyTaFJko4GDL22g 3y/rSDwqR+7OLyq6MBmXHJyJgpZEY9xf9GaHkSl9WslnnzvDI/YaNLsGR61dZ5sTRkrO CniSgKoc/dujI3fI8GqgeX0CWqZ30eFHE7OrUGbIf750yj+0mbB6xpfj+11G57kJwNLd 0V0m7HFJlMPs6sWBVy4um3M23NHUiebk5msW6WGVTWFw1VgDmrx77v8l1LlzMQ29lVgf 6UXP1uK/TOnjJ3lyyRvlP2IZKE7o92A4r7aF+wa/Qpd0nLxrKP2cVvKy/JoOQjAlvbFj oOAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:subject:from:to:date:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=T99iOa1o5CiFTXaeT/jUV6D5RLEpc6JSqxaZqfkYVoU=; b=XvFo+psVvt5n5ukzFTQuiOiaA/OuxPRG1QSm+Xa//4igiWmcB4Zng91f7HDlP5exUd Sxk9oMhSG7k3twGDplDviA2O1NFrN2MNuN16djPu7fXqcWAKlLXhM6a8sjLiIig4wSd1 H32UG8538GlnvYSJsMXQTLcStfEB/8002kkK1oX8i5ReCV5TKAXChXO+sgitL0HBOwkO 0N2jTyqN3B2IMqv7v72a65G2vLsT4SVpdn4MHCoOuEJzKsnU3qm63bDrDGW2kIe6f2qx vGP9lo2rIjlr2VMFiTMoHD0envUd59jTUCyPThhgjK4sLsRrAlOGfWC0L3mvzwW4QOGX 4kvw== X-Gm-Message-State: AOAM532Nk4nus3tTDQUEVEuLil/kCMB3haVcrvTwYvoCBR2bFJ3Md+1T 3T1xrMS5vVwK6FFm/jIn09+QLOiKHxFxTw== X-Google-Smtp-Source: ABdhPJzeRxrpTH6L32ImEIoN4Rlv0pNDyB4XbnyavaQst8qWEgXcvZi9XdWbuArt9uusNRSbdD9Fjg== X-Received: by 2002:a1c:f30f:: with SMTP id q15mr7229485wmq.60.1599221657009; Fri, 04 Sep 2020 05:14:17 -0700 (PDT) Received: from localhost.localdomain (217-149-169-204.nat.highway.telekom.at. [217.149.169.204]) by smtp.gmail.com with ESMTPSA id g7sm12166002wrv.82.2020.09.04.05.14.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Sep 2020 05:14:16 -0700 (PDT) Message-ID: Subject: Re: Bitmap scan seem like such a strange choice when "limit 1" From: Laurenz Albe To: Klaudie Willis , PostgreSQL General Date: Fri, 04 Sep 2020 14:14:15 +0200 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.36.5 (3.36.5-1.fc32) MIME-Version: 1.0 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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