Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1epVSa-0002Mu-Ua for pgsql-performance@arkaria.postgresql.org; Sat, 24 Feb 2018 08:45:37 +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 1epVSZ-00010F-VS for pgsql-performance@arkaria.postgresql.org; Sat, 24 Feb 2018 08:45:35 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1epVSZ-000106-MX for pgsql-performance@lists.postgresql.org; Sat, 24 Feb 2018 08:45:35 +0000 Received: from mail-lf0-x234.google.com ([2a00:1450:4010:c07::234]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1epVSJ-0001T9-5N for pgsql-performance@lists.postgresql.org; Sat, 24 Feb 2018 08:45:34 +0000 Received: by mail-lf0-x234.google.com with SMTP id 37so15595617lfs.7 for ; Sat, 24 Feb 2018 00:45:18 -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-transfer-encoding:content-language; bh=I/mNpSVCB7Cv89gfxOT/l/5D9mXzvuh/NRe8AcY4TiE=; b=t+BFhgmUJB1EP8U9gan9969VVdacn5uUQaW7oloRoHrARMX9wXPsUJajZrfecfptkI oGnCDmtCRF5SX8QAjSYXm/BtvkBxvSGJwKn+tXyZvZ9wjIHztAePO0h8UieY45adxXbP PiAj7d+V/suPGuVKzhnwiwSBiB0YJm+F7rOTJKV8EaCKAsCdbVjqisAkVmSl4zpa0GAm tvenSTnlN/7HAI3i5OyKWrvMzIu8y4X72UWaPnqnuOJ8g5qaFBzIHZR+m6R481lX9Dwl mMi/ivyDeEuqO9x/2VTxXzJ3P28+sutL83U8KMVv09WgivvRCxiKp6Y3RyhpbA6rIE8B bHbg== 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-transfer-encoding :content-language; bh=I/mNpSVCB7Cv89gfxOT/l/5D9mXzvuh/NRe8AcY4TiE=; b=MJPFSqjnGeed3apYBN9ObiXc0dacee4wf2Rjt7pGmmVt8KUn/Etpousik3pkUjD/Qb Oyfxq9RA7PjKBc+XBgNcBNYU1OGhnNifTsUNIdUwwUEMswho7dEXnryyfalVN5oWIqyR fGt0HwaTcZSQJrcT0XG3h1AqKfph8O0pmbS0h3Byhcl/s5yJjxib2H+FJPjJt+hYcf5b CRxExXnm1xhPGwHGgPm+hSe9PPn7ApH59jrMwZJproy0EhpDZhIYJ7SuhUqWKOLSWg3u cl36yMQgnLBfVFMGIiVkgftj5KYqMg8sFltNAfpKGVbh5zbFfpG+Yuptn/GYS/jV4fvo ZMlA== X-Gm-Message-State: APf1xPC5klGrKiIJissbnuR5BoYD6/tPwkNKHFHB1Bze4rl7A35KOFMv p02onZW1E/crBB0IzCLvJMesQX7k X-Google-Smtp-Source: AG47ELtHdmESGIiuPW1xcNMMlv6kqPu4N0jTtsPgZPi2he7XgV4F81MVCvVnZ9bdDrVPHnfgevOdzw== X-Received: by 10.46.114.26 with SMTP id n26mr3351451ljc.74.1519461916470; Sat, 24 Feb 2018 00:45:16 -0800 (PST) Received: from [192.168.1.151] ([178.54.41.83]) by smtp.gmail.com with ESMTPSA id k184sm841963lfg.72.2018.02.24.00.45.15 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 24 Feb 2018 00:45:15 -0800 (PST) Subject: Re: Bitmap scan is undercosted? To: Jeff Janes Cc: Tom Lane , 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> <0a0bfd5f-06cc-918b-da75-4e45c4e8ea27@gmail.com> From: Vitaliy Garnashevich Message-ID: Date: Sat, 24 Feb 2018 10:45:14 +0200 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Sorry for delay with response, I had to switch to other tasks and didn't have time to run proper tests and write some meaningful response. Recently,  a similar issue happened with another our database, so I decided to write an update. Bitmap scan was preferred to index scan by the planner, but bitmap scan was running worse in practice. Here are the relevant pieces of a much bigger query plan:  ->  Bitmap Heap Scan on cmdb_program_daily_usage cmdb_program_daily_usage_6  (cost=6707.08..6879.35 rows=32 width=20) (actual time=39.994..40.019 rows=12 loops=336)        Recheck Cond: ((used_from = cmdb_ci_computer_12.id) AND (usage_date >= '2018-02-02'::date) AND (usage_date <= '2018-02-12'::date))        Filter: (((NOT thin_client) OR (thin_client IS NULL)) AND (program_instance IS NOT NULL) AND (minutes_in_use > 0))        Rows Removed by Filter: 69        Heap Blocks: exact=2995        Buffers: shared hit=563448        ->  BitmapAnd  (cost=6707.08..6707.08 rows=154 width=0) (actual time=39.978..39.978 rows=0 loops=336)              Buffers: shared hit=560453              ->  Bitmap Index Scan on idx_fk_5317241949468942  (cost=0.00..133.87 rows=12641 width=0) (actual time=0.373..0.373 rows=4780 loops=336)                    Index Cond: (used_from = cmdb_ci_computer_12.id)                    Buffers: shared hit=5765              ->  Bitmap Index Scan on idx_263911642415136  (cost=0.00..6572.94 rows=504668 width=0) (actual time=40.873..40.873 rows=540327 loops=324)                    Index Cond: ((usage_date >= '2018-02-02'::date) AND (usage_date <= '2018-02-12'::date))                    Buffers: shared hit=554688  ->  Index Scan using idx_fk_5317241949468942 on cmdb_program_daily_usage cmdb_program_daily_usage_6 (cost=0.56..24322.97 rows=35 width=20) (actual time=1.211..2.196 rows=14 loops=338)        Index Cond: (used_from = cmdb_ci_computer_12.id)        Filter: (((NOT thin_client) OR (thin_client IS NULL)) AND (program_instance IS NOT NULL) AND (minutes_in_use > 0) AND (usage_date >= '2018-02-02'::date) AND (usage_date <= '2018-02-12'::date))        Rows Removed by Filter: 4786        Buffers: shared hit=289812 The difference in run time does not look very huge, but when it's a part of a loop, that could mean difference between minutes and hours. After running some tests, here are the conclusions we've made: - When running with cold cache, and data is being read from disk, then the planner estimates look adequate. Bitmap scan has better costs, and indeed it performs better in that case. - When running with hot cache, and most of data is already in RAM, then index scan starts to outperform bitmap scan. Unfortunately the planner cannot account for the cache very well, and can't switch the plan. Because even if the planner would ever learn to account for the current content of shared buffers, it still can't know much about the content of filesystem cache. - Tests showed that the costs are dominated by random_page_cost, but there is still potential to change the total plan cost, if "cpu_*" costs would be less distant from "*_page_cost". - In our case the data is likely to be in cache, so we decided to change cost settings: seq_page_cost 1.0 -> 0.5; random_page_cost 1.1 -> 0.6 Regards, Vitaliy