Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eLcUL-0000f0-OE for pgsql-performance@arkaria.postgresql.org; Sun, 03 Dec 2017 22:11:53 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eLcUL-0001xD-15 for pgsql-performance@arkaria.postgresql.org; Sun, 03 Dec 2017 22:11:53 +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.84_2) (envelope-from ) id 1eLcUK-0001x4-Lb for pgsql-performance@lists.postgresql.org; Sun, 03 Dec 2017 22:11:52 +0000 Received: from mail-lf0-x242.google.com ([2a00:1450:4010:c07::242]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1eLcUI-0001X4-3v for pgsql-performance@lists.postgresql.org; Sun, 03 Dec 2017 22:11:51 +0000 Received: by mail-lf0-x242.google.com with SMTP id x20so17076422lff.1 for ; Sun, 03 Dec 2017 14:11:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language; bh=moBeUIUtEaLP1qamS+S34r/ypENunxnG3kzEoReWKh0=; b=baWDq6Uy9sDU4s5E/tZMyHjDSCe5Q2fQIey7j/lXV5VBfSyFrq5Fc9vjMesM1Htcx5 KtABwEQkQiJacWFgPzxCkBwB2hSVBKc9eZk8VvriXZYSytV/vye8pMgZH0+t+wcj6BAT oq+3zVvamoeqIi3z5ZyjN8S/NcePU08TZsrG51ao1oKebMngmWFptuEz5dl5agHyt4dt 0TR+L5BnjMi2vtsyxUcsBWaBLndsuMI7h1pYpjiloLFhHymm4j0wxrnypmWNNr+DzHwC H+PasscARiXku6hb1J8Yehcua1n6bX1twd8vWYaEHIHZAbszqfQfX7zYbrDKdfF9o99H FA/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=moBeUIUtEaLP1qamS+S34r/ypENunxnG3kzEoReWKh0=; b=LX6ZnlLOydMVy4fVP4K5NH5HjRrjxRe0+cPmN18yXFXJjKcuJk1eP+PvFegksMlIuF uOE5x9u6qgYZr4Tocy6RnfyMte9jXV+peyQb1Y6N+XI3amMzUlt1nO4wv0A0oJGt3fKG B/GsD+XGGRYNhZZZohpbNB+2gFdZfBTW2bbMhFB4w6HqGEWK4nYauvwTNWZE0qgRQZYo VlCNSGZ83Lh4WcLgEK8Q4dH29Tcar0Q++O/MHZqPlQ5uG5F1L1ssd3UTW9j47QQRfkzm 129ZdN1XtGDBx/aw+hVvwf7cLQe27SB7CKG0gyuYV15sgzxoEIyF5t8zNi1aEI1NvtBc HN0A== X-Gm-Message-State: AJaThX54MPtGtGuw3KiQ1eHNv4Ru+J9ZFdokQKBONB3gh9tiAoXvCHpj l7oVH9LhJ3gZZym1Ydm1wjq3y+pm X-Google-Smtp-Source: AGs4zMZEWDO/N1AdSwUyE7NP2to+fido7GQ9RJl+Zpf5bo5LUah9F0Y2UginD7IWdDk2G+rCzstM0g== X-Received: by 10.46.58.22 with SMTP id h22mr8046584lja.194.1512339107226; Sun, 03 Dec 2017 14:11:47 -0800 (PST) Received: from [192.168.1.151] ([178.54.41.83]) by smtp.gmail.com with ESMTPSA id c1sm2440211ljb.78.2017.12.03.14.11.45 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 03 Dec 2017 14:11:46 -0800 (PST) Subject: Re: Bitmap scan is undercosted? To: Jeff Janes , Tom Lane Cc: Justin Pryzby , pgsql-performance@lists.postgresql.org References: <73a4936d-2814-dc08-ed0c-978f76f435b0@gmail.com> <20171201183427.GK18413@telsasoft.com> <20171201231104.GN18413@telsasoft.com> <18188.1512258290@sss.pgh.pa.us> From: Vitaliy Garnashevich Message-ID: <0a0bfd5f-06cc-918b-da75-4e45c4e8ea27@gmail.com> Date: Mon, 4 Dec 2017 00:11:47 +0200 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.5.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------680E8145918951260F30BBA8" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------680E8145918951260F30BBA8 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit On 03/12/2017 03:27, Jeff Janes wrote: > Due to that, when I disable bitmapscans and seqscans, I start getting > slow index scans on the wrong index, i2 rather than i1.  I don't know > why he doesn't see that in his example. When I increase effective_cache_size to 1024MB, I start getting the plan with the slower index i2, too. *Bitmap Heap Scan* on public.aaa  (cost=12600.90..*23688**.70* rows=9488 width=5) (actual time=107.529..*115.902* rows=9976 loops=1)   ->  BitmapAnd  (cost=12600.90..12600.90 rows=9488 width=0) (actual time=105.133..105.133 rows=0 loops=1)         ->  Bitmap Index Scan on i1  (cost=0.00..1116.43 rows=96000 width=0) (actual time=16.313..16.313 rows=100508 loops=1)         ->  Bitmap Index Scan on i2  (cost=0.00..11479.47 rows=988338 width=0) (actual time=77.950..77.950 rows=1000200 loops=1) *Index Scan* using i2 on public.aaa  (cost=0.44..*48227.31* rows=9488 width=5) (actual time=0.020..*285.695* rows=9976 loops=1) *Seq Scan* on public.aaa  (cost=0.00..*169248.54* rows=9488 width=5) (actual time=0.024..*966.469* rows=9976 loops=1) This way the estimates and the actual time get more sense. But then there's the question - maybe it's i1 runs too fast, and is estimated incorrectly? Why that happens? Here are the complete plans with the two different kinds of index scans once again: Index Scan using i1 on public.aaa  (cost=0.44..66621.56 rows=10340 width=5) (actual time=0.027..47.075 rows=9944 loops=1)   Output: num, flag   Index Cond: (aaa.num = 1)   Filter: aaa.flag   Rows Removed by Filter: 89687   Buffers: shared hit=39949 Planning time: 0.104 ms Execution time: 47.351 ms Index Scan using i2 on public.aaa  (cost=0.44..48227.31 rows=9488 width=5) (actual time=0.020..285.695 rows=9976 loops=1)   Output: num, flag   Index Cond: (aaa.flag = true)   Filter: (aaa.flag AND (aaa.num = 1))   Rows Removed by Filter: 990224   Buffers: shared hit=46984 Planning time: 0.098 ms Execution time: 286.081 ms // The test DB was populated with: create table aaa as select floor(random()*100)::int num, (random()*10 < 1)::bool flag from generate_series(1, 10000000) id; Regards, Vitaliy --------------680E8145918951260F30BBA8 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit
On 03/12/2017 03:27, Jeff Janes wrote:
Due to that, when I disable bitmapscans and seqscans, I start getting slow index scans on the wrong index, i2 rather than i1.  I don't know why he doesn't see that in his example.
When I increase effective_cache_size to 1024MB, I start getting the plan with the slower index i2, too.

Bitmap Heap Scan on public.aaa  (cost=12600.90..23688.70 rows=9488 width=5) (actual time=107.529..115.902 rows=9976 loops=1)
  ->  BitmapAnd  (cost=12600.90..12600.90 rows=9488 width=0) (actual time=105.133..105.133 rows=0 loops=1)
        ->  Bitmap Index Scan on i1  (cost=0.00..1116.43 rows=96000 width=0) (actual time=16.313..16.313 rows=100508 loops=1)
        ->  Bitmap Index Scan on i2  (cost=0.00..11479.47 rows=988338 width=0) (actual time=77.950..77.950 rows=1000200 loops=1)

Index Scan using i2 on public.aaa  (cost=0.44..48227.31 rows=9488 width=5) (actual time=0.020..285.695 rows=9976 loops=1)

Seq Scan on public.aaa  (cost=0.00..169248.54 rows=9488 width=5) (actual time=0.024..966.469 rows=9976 loops=1)

This way the estimates and the actual time get more sense. But then there's the question - maybe it's i1 runs too fast, and is estimated incorrectly? Why that happens?

Here are the complete plans with the two different kinds of index scans once again:

Index Scan using i1 on public.aaa  (cost=0.44..66621.56 rows=10340 width=5) (actual time=0.027..47.075 rows=9944 loops=1)
  Output: num, flag
  Index Cond: (aaa.num = 1)
  Filter: aaa.flag
  Rows Removed by Filter: 89687
  Buffers: shared hit=39949
Planning time: 0.104 ms
Execution time: 47.351 ms

Index Scan using i2 on public.aaa  (cost=0.44..48227.31 rows=9488 width=5) (actual time=0.020..285.695 rows=9976 loops=1)
  Output: num, flag
  Index Cond: (aaa.flag = true)
  Filter: (aaa.flag AND (aaa.num = 1))
  Rows Removed by Filter: 990224
  Buffers: shared hit=46984
Planning time: 0.098 ms
Execution time: 286.081 ms


// The test DB was populated with: create table aaa as select floor(random()*100)::int num, (random()*10 < 1)::bool flag from generate_series(1, 10000000) id;

Regards,
Vitaliy

--------------680E8145918951260F30BBA8--