Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cGszo-0002R5-O3 for pgsql-hackers@arkaria.postgresql.org; Tue, 13 Dec 2016 19:44:16 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1cGszn-0007ii-Ql for pgsql-hackers@arkaria.postgresql.org; Tue, 13 Dec 2016 19:44:15 +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 1cGszm-0007iZ-3s for pgsql-hackers@postgresql.org; Tue, 13 Dec 2016 19:44:14 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1cGszj-0000H5-Hl for pgsql-hackers@postgresql.org; Tue, 13 Dec 2016 19:44:12 +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 uBDJi7ZL023546; Tue, 13 Dec 2016 14:44:07 -0500 From: Tom Lane To: ronan.dunklau@dalibo.com cc: pgsql-hackers@postgresql.org, Tomas Vondra , Adrien Nayrat Subject: Re: New design for FK-based join selectivity estimation In-reply-to: <3012483.BymkNIPMEm@ronan_laptop> References: <31041.1465069446@sss.pgh.pa.us> <13294.1466278050@sss.pgh.pa.us> <9e35d4cb-a12d-7987-e23a-8a6020e01e88@dalibo.com> <3012483.BymkNIPMEm@ronan_laptop> Comments: In-reply-to ronan.dunklau@dalibo.com message dated "Tue, 13 Dec 2016 19:39:23 +0100" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <23544.1481658247.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Tue, 13 Dec 2016 14:44:07 -0500 Message-ID: <23545.1481658247@sss.pgh.pa.us> X-Pg-Spam-Score: -5.0 (-----) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org ronan.dunklau@dalibo.com writes: > On mardi 13 d=C3=A9cembre 2016 09:10:47 CET Adrien Nayrat wrote: >> The commit 100340e2dcd05d6505082a8fe343fb2ef2fa5b2a introduce an >> estimation error : > The problem is, for semi and anti joins, we assume that we have nohing to= do=20 > (costsize.c:4253): > else if (jointype =3D=3D JOIN_SEMI || jointype =3D=3D JOIN_ANTI) > { > /* > * For JOIN_SEMI and JOIN_ANTI, the selectivity is defined as the > * fraction of LHS rows that have matches. If the referenced > * table is on the inner side, that means the selectivity is 1.0 > * (modulo nulls, which we're ignoring for now). We already > * covered the other case, so no work here. > */ > } > This results in assuming that the whole outerrel will match, no matter th= e=20 > selectivity of the innerrel. Yeah. In the terms of this example, the FK means that every outer row would have a match, if the query were select * from t3 where j in (select * from t4); But actually it's select * from t3 where j in (select * from t4 where j<10); so of course we should not expect a match for every row. > If I understand it correctly and the above is right, I think we should ig= nore=20 > SEMI or ANTI joins altogether when considering FKs, and keep the correspo= nding=20 > restrictinfos for later processing since they are already special-cased l= ater=20 > on.=20 That seems like an overreaction. While the old code happens to get this example exactly right, eqjoinsel_semi is still full of assumptions and approximations, and it doesn't do very well at all if it lacks MCV lists for both sides. I'm inclined to think that what we want to have happen in this case is to estimate the fraction of outer rows having a match as equal to the selectivity of the inner query's WHERE clauses, ie the semijoin selectivity should be sizeof(inner result) divided by sizeof(inner relation). regards, tom lane --=20 Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers