Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eLbj3-0006H7-57 for pgsql-performance@arkaria.postgresql.org; Sun, 03 Dec 2017 21:23:01 +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 1eLbj2-0003ci-8T for pgsql-performance@arkaria.postgresql.org; Sun, 03 Dec 2017 21:23:00 +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 1eLbj1-0003cY-Qc for pgsql-performance@lists.postgresql.org; Sun, 03 Dec 2017 21:23:00 +0000 Received: from mail-lf0-x243.google.com ([2a00:1450:4010:c07::243]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1eLbix-0000W9-23 for pgsql-performance@lists.postgresql.org; Sun, 03 Dec 2017 21:22:58 +0000 Received: by mail-lf0-x243.google.com with SMTP id x204so16960339lfa.11 for ; Sun, 03 Dec 2017 13:22:54 -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=tbrBkFSJU6BStuKoP0Ux3m85SMOylY0rMNWjTZiDsIk=; b=Nybwaal+BT5TlRbT6ehcgaNadC2/Lx9h+Kj6SpCh+A1nPFcBahRbelKOBXQ8IIXd8k ymfi+gjXCdSMYs+7l9sXdWFmFXZynnipQXgAaFllP6l6NVO0lOAZdr/RbfpvseRSx6nc 5ktEE1OFK1noAx9yYMcsIxzcEkQdc87mp9rHOLkwtygfy3WZOhTz+Kym6zJr3hKxwlA7 mkLT3Tn+nNAL6wl6ur6gDIoC1f0MDk4eAVQIjO+Hh9cW/vTlAIGW2Yzn+VQ92MTlr2vP FkxCVaDJc4APF6I9IW4LaFYfSn2elz0Ff/8S5Xui2APXVXp9N+wPDF+dM8kKvp+ZBv7V dkJQ== 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=tbrBkFSJU6BStuKoP0Ux3m85SMOylY0rMNWjTZiDsIk=; b=rBNjlLc8AtLFzjp+UPBf1vxyrR6YGlRYNzgHCBcGvFIzUOmV6X/EFLqIm/Vk2lH9E+ zFSnzpUSXbkbbF5ca6G9Tw0QyCWhxjUB6Vblw1tXhh5vfJAWgfzsO/0af9kn+FsHo9tz yjho4yiDStPnX6oNulYx5utzzLXcxyPewUjbv0BV/36sI9Q8PA+fYctXNAaeq4w5JO94 R9ZbjafdkZoxITRS2Vvni4BYxxaPGOgXS5E7WmbNtdD7PKqmjqG+27e0czpTP40hvwB0 u81XldPMENDouJ874S4MtIC2JIbwZOG4DliHhVt11dMHj6atuattXojND+FXC3tzb7cE sQTA== X-Gm-Message-State: AJaThX6P4zZSpjwdZSsbCOTuiTlaRmiPjkjaFihOfSC3+D7sOGcEHjam 7TcdFCCdL3g8Z0V5PJXtNUybSQWu X-Google-Smtp-Source: AGs4zMbaSFoke2pySfpzWCzSzJCBTjdR++sdSLK5Fs1q+mbrSdDS2RG4dR+qQILRZW3/fw+6RBF4bA== X-Received: by 10.25.59.28 with SMTP id i28mr6560318lfa.189.1512336172592; Sun, 03 Dec 2017 13:22:52 -0800 (PST) Received: from [192.168.1.151] ([178.54.41.83]) by smtp.gmail.com with ESMTPSA id f67sm2097599lfb.20.2017.12.03.13.22.51 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 03 Dec 2017 13:22:52 -0800 (PST) Subject: Re: Bitmap scan is undercosted? To: Tom Lane , Jeff Janes 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: <84b6f7b5-df1a-94be-9b82-5a2756119ebe@gmail.com> Date: Sun, 3 Dec 2017 23:22:52 +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: <18188.1512258290@sss.pgh.pa.us> 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 On 03/12/2017 01:44, Tom Lane wrote: > I think it'd be a serious error to screw around with your cost settings > on the basis of a single case in which the rowcount estimates are so > far off. It's really those estimates that are the problem AFAICS. > > The core issue in this example is that, the way the test data is set up, > the "flag = true" condition actually adds no selectivity at all, because > every row with "num = 1" is certain to have "flag = true". If the planner > realized that, it would certainly not bother with BitmapAnd'ing the flag > index onto the results of the num index. But it doesn't know that those > columns are correlated, so it supposes that adding the extra index will > give a 10x reduction in the number of heap rows that have to be visited > (since it knows that only 1/10th of the rows have "flag = true"). > *That* is what causes the overly optimistic cost estimate for the > two-index bitmapscan, and no amount of fiddling with the cost parameters > will make that better. Here I've tried to make a test which would not have correlation between the two columns. shared_buffers = 512MB effective_cache_size = 512MB work_mem = 100MB set seq_page_cost = 1.0; set random_page_cost = 1.5; set cpu_tuple_cost = 0.01; set cpu_index_tuple_cost = 0.005; set cpu_operator_cost = 0.0025; drop table if exists aaa; create table aaa as select floor(random()*100)::int num, (random()*10 < 1)::bool flag from generate_series(1, 10000000) id; create index i1 on aaa  (num); create index i2 on aaa  (flag); analyze aaa; select relname, reltuples::bigint, relpages::bigint, (reltuples/relpages)::bigint tpp from pg_class where relname in('aaa','i1','i2') order by relname; "aaa";10000033;44248;226 "i1";10000033;27422;365 "i2";10000033;27422;365 select flag, count(*) from aaa group by flag order by flag; f;9000661 t;999339 select num, count(*) from aaa group by num order by num; 0;99852 1;99631 2;99699 3;100493 ... 96;100345 97;99322 98;100013 99;100030 explain (analyze,verbose,costs,buffers) select * from aaa where num = 1 and flag = true; Bitmap Heap Scan on public.aaa  (cost=12829.83..24729.85 rows=10340 width=5) (actual time=104.941..112.649 rows=9944 loops=1)   Output: num, flag   Recheck Cond: (aaa.num = 1)   Filter: aaa.flag   Heap Blocks: exact=8922   Buffers: shared hit=11932   ->  BitmapAnd  (cost=12829.83..12829.83 rows=10340 width=0) (actual time=102.926..102.926 rows=0 loops=1)         Buffers: shared hit=3010         ->  Bitmap Index Scan on i1  (cost=0.00..1201.44 rows=103334 width=0) (actual time=15.459..15.459 rows=99631 loops=1)               Index Cond: (aaa.num = 1)               Buffers: shared hit=276         ->  Bitmap Index Scan on i2  (cost=0.00..11622.97 rows=1000671 width=0) (actual time=76.906..76.906 rows=999339 loops=1)               Index Cond: (aaa.flag = true)               Buffers: shared hit=2734 Planning time: 0.110 ms Execution time: 113.272 ms 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 > > I tried creating multiple-column statistics using the v10 facility for > that: > > regression=# create statistics s1 on num, flag from aaa; > CREATE STATISTICS > regression=# analyze aaa; > ANALYZE > > but that changed the estimate not at all, which surprised me because > dependency statistics are supposed to fix exactly this type of problem. > I suspect there may be something in the extended-stats code that causes it > not to work right for boolean columns --- this wouldn't be excessively > surprising because of the way the planner messes around with converting > "flag = true" to just "flag" and sometimes back again. But I've not > looked closer yet. > > regards, tom lane > . >