From: Tom Lane <tgl@sss.pgh.pa.us>
To: Andres Freund <andres@anarazel.de>
Cc: pgsql-bugs@postgresql.org
Cc: casey.shobe@messagesystems.com
Subject: Re: Botched estimation in eqjoinsel_semi for cases without reliable ndistinct
Date: Wed, 15 Feb 2012 19:24:59 -0500
Message-ID: <26313.1329351899@sss.pgh.pa.us> (raw)
In-Reply-To: <201201120300.03698.andres@anarazel.de>
References: <201201112110.40403.andres@anarazel.de>
<201201120140.35229.andres@anarazel.de>
<1378.1326331484@sss.pgh.pa.us>
<201201120300.03698.andres@anarazel.de>
[ getting back to this after assorted distractions ]
Andres Freund <andres@anarazel.de> writes:
> On Thursday, January 12, 2012 02:24:44 AM Tom Lane wrote:
>> Andres Freund <andres@anarazel.de> writes:
>>> On Thursday, January 12, 2012 01:01:01 AM Tom Lane wrote:
>>>> Looks pretty bogus to me. You're essentially assuming that the side of
>>>> the join without statistics is unique, which is a mighty dubious
>>>> assumption.
>>> It sure is a bit dubious. But assuming that a semijoin that has max of n
>>> rows on the inner side results in half of the outer sides rows (>> n) is
>>> pretty bogus as well.
> In the current example we have an estimate for the distinctness of the LHS. I
> don't see how guesstimating the RHS number of tuples in a semijoin to
> vardata2->rel->rows will be worse than just assuming a selectivity of 0.5 for
> the whole thing.
The real problem is that the estimate we want to use involves the ratio
nd2/nd1. If either of those numbers is mere fantasy, so is the ratio.
It doesn't really help to say "well, we can upper-bound this number
here", because sometimes a too large result is as bad as too small.
>> One thing I've considered but not done anything about is that in a lot
>> of practical cases for this, the aggregation or grouping properties of
>> the sub-select would provide adequate reason for assuming its output is
>> more or less unique, so that taking ndistinct equal to number of rows
>> actually is sane. But it would need a bit of thought about what
>> properties we want to treat as justifying such an assumption, and then
>> some code to see if the join key is a Var coming out of such a
>> sub-select. (Actually, what such a patch would probably look like is
>> modifying examine_simple_variable to not just punt when it finds the
>> Var came from an aggregating subquery.)
> Yes, having that would be great, but be a bit more invasive than I like to
> think right now. This thing is actually a problem for people in the field...
I'm not sure it's that bad. Attached is a simple patch to notice that
the subquery is "SELECT DISTINCT foo" and if so assume the result is
unique. This takes care of at least the first of your original
examples. I'm not sure whether fixing this single case is enough to get
excited about, but it's a step forward anyway.
Your second example, involving a WITH, would properly be handled by
teaching examine_simple_variable to drill down into CTEs. I lacked the
round tuits to make it do so initially, and still do, but if you'd like
to have a go at it ...
regards, tom lane
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: tgl@sss.pgh.pa.us, andres@anarazel.de, casey.shobe@messagesystems.com
Subject: Re: Botched estimation in eqjoinsel_semi for cases without reliable ndistinct
In-Reply-To: <26313.1329351899@sss.pgh.pa.us>
References: <201201112110.40403.andres@anarazel.de> <201201120140.35229.andres@anarazel.de> <1378.1326331484@sss.pgh.pa.us> <201201120300.03698.andres@anarazel.de> <26313.1329351899@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox