Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g21kP-0002uj-M0 for pgsql-general@arkaria.postgresql.org; Mon, 17 Sep 2018 22:12:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1g21kN-0003pC-Om for pgsql-general@arkaria.postgresql.org; Mon, 17 Sep 2018 22:11:59 +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.89) (envelope-from ) id 1g21kN-0003p5-H8 for pgsql-general@lists.postgresql.org; Mon, 17 Sep 2018 22:11:59 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g21kK-0004GR-S7 for pgsql-general@postgresql.org; Mon, 17 Sep 2018 22:11:59 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id w8HMBqIO028693; Mon, 17 Sep 2018 18:11:53 -0400 From: Tom Lane To: Seamus Abshere cc: pgsql-general@postgresql.org Subject: Re: Too many BitmapAnds in the wild In-reply-to: <1537212871.286640.1511233968.66591B19@webmail.messagingengine.com> References: <1537212871.286640.1511233968.66591B19@webmail.messagingengine.com> Comments: In-reply-to Seamus Abshere message dated "Mon, 17 Sep 2018 16:34:31 -0300" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <28691.1537222312.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Mon, 17 Sep 2018 18:11:52 -0400 Message-ID: <28692.1537222312@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Seamus Abshere writes: > hey, > We see a fair number of incorrectly chosen BitmapAnd plans in the wild a= t Faraday... enough that googling the problem ends up at our old posts to = this mailing list =C3=B0=C2=9F=C2=98=C2=80. An attractive solution was pro= posed by Jeff Janes [1] > - *cost +=3D 0.1 * cpu_operator_cost * path->rows; > + *cost +=3D 6 * cpu_operator_cost * path->rows; > It appears this constant hasn't been changed for 7 years [2]. AFAICT you are pointing at this: cost_bitmap_tree_node(Path *path, Cost *cost, Selectivity *selec) { if (IsA(path, IndexPath)) { *cost =3D ((IndexPath *) path)->indextotalcost; *selec =3D ((IndexPath *) path)->indexselectivity; /* * Charge a small amount per retrieved tuple to reflect the costs = of * manipulating the bitmap. This is mostly to make sure that a bi= tmap * scan doesn't look to be the same cost as an indexscan to retrie= ve a * single tuple. */ *cost +=3D 0.1 * cpu_operator_cost * path->rows; } which does not seem like the place to be putting your thumb on the scale if you want to discourage bitmap ANDs. That would increase the estimate for plain bitmap scans as well as ANDs. Moreover, there's no plausible reasoning for this adjustment being more than a minimal one. There's a different fudge factor in cost_bitmap_and_node (also in cost_bitmap_or_node) that would probably be more plausible to twiddle: * The runtime cost of the BitmapAnd itself is estimated at 100x * cpu_operator_cost for each tbm_intersect needed. Probably too smal= l, * definitely too simplistic? if (l !=3D list_head(path->bitmapquals)) totalCost +=3D 100.0 * cpu_operator_cost; I'd be the first to say that that number has no experimental basis, plus modeling it as a quasi-constant is theoretically wrong; surely it ought to vary depending on how big we think the bitmap might be. I'm not, however, very enamored of just replacing the "100" with some other random constant without any evidence to back up the change. Let's see some test cases, at least. regards, tom lane