Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Uryl5-0001H5-M3 for pgsql-performance@arkaria.postgresql.org; Wed, 26 Jun 2013 23:04:15 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Uryl5-00042p-5u for pgsql-performance@arkaria.postgresql.org; Wed, 26 Jun 2013 23:04:15 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Uryl3-00042i-UB for pgsql-performance@postgresql.org; Wed, 26 Jun 2013 23:04:14 +0000 Received: from smtp.01.com ([199.36.142.181] helo=smtp-out-1.01.com) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Urykx-0000R3-Hh for pgsql-performance@postgresql.org; Wed, 26 Jun 2013 23:04:13 +0000 Received: from localhost (localhost [127.0.0.1]) by smtp-out-1.01.com (Postfix) with ESMTP id EF0A229400A; Wed, 26 Jun 2013 18:04:06 -0500 (CDT) X-Virus-Scanned: amavisd-new at smtp-out-1.01.com Received: from smtp-out-1.01.com ([127.0.0.1]) by localhost (smtp-out-1.01.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 41O_xCa1bfa2; Wed, 26 Jun 2013 18:04:06 -0500 (CDT) Received: from smtp-out-1.01.com (localhost [127.0.0.1]) by smtp-out-1.01.com (Postfix) with ESMTP id C434029403C; Wed, 26 Jun 2013 18:04:06 -0500 (CDT) Received: from localhost (localhost [127.0.0.1]) by smtp-out-1.01.com (Postfix) with ESMTP id ACEE5294033; Wed, 26 Jun 2013 18:04:06 -0500 (CDT) X-Virus-Scanned: amavisd-new at smtp-out-1.01.com Received: from smtp-out-1.01.com ([127.0.0.1]) by localhost (smtp-out-1.01.com [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id x9juQFBD0lGX; Wed, 26 Jun 2013 18:04:06 -0500 (CDT) Received: from [172.47.23.103] (70-36-143-10.dsl.dynamic.sonic.net [70.36.143.10]) by smtp-out-1.01.com (Postfix) with ESMTPSA id 466BB29400A; Wed, 26 Jun 2013 18:04:05 -0500 (CDT) Message-ID: <51CB7362.5060605@agliodbs.com> Date: Wed, 26 Jun 2013 16:04:02 -0700 From: Josh Berkus Organization: PostgreSQL Experts Inc. User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130510 Thunderbird/17.0.6 MIME-Version: 1.0 To: Tom Lane CC: "pgsql-performance@postgresql.org" Subject: Re: Weird, bad 0.5% selectivity estimate for a column equal to itself References: <51C4AEE5.8040704@agliodbs.com> <25930.1371850378@sss.pgh.pa.us> <51C4D1E8.6090602@agliodbs.com> <24522.1371932688@sss.pgh.pa.us> <51CA2370.7040803@agliodbs.com> <22653.1372210907@sss.pgh.pa.us> In-Reply-To: X-Enigmail-Version: 1.5.1 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: 0.8 (/) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org On 06/25/2013 06:41 PM, Tom Lane wrote: >> Well, it was more in the form of: >> tab1.x = COALESCE(tab2.y,tab1.x) > > Hm. I'm not following how you get from there to complaining about not > being smart about X = X, because that surely ain't the same. Actually, it was dominated by defaultsel, since tab2.y had a nullfrac of 70%. It took us a couple days of reducing the bad query plan to figure out where the bad estimate was coming from. The real estimate should have been 0.7 + ( est. tab2.y = tab1.x ), but instead we were getting 0.005 + ( est. tab2.y = tab1.x ), which was throwing the whole query plan way off ... with an execution time difference of 900X. > It's a totally different case. In the join case you expect that each > element of one table will be compared with each element of the other. > In the single-table case, that's exactly *not* what will happen, and > I don't see how you get to anything very useful without knowing > something about the value pairs that actually occur. Sure you can. If you make the assumption that there is 0 correlation, then you can simply estimate the comparison as between two random columns. In the simplest approach, you would multiply the two ndistincts, so that a column with 3 values would match a column with 10 values 0.033 of the time. Now for a real estimator, we'd of course want to use the MCVs and the histogram to calculate a better estimation; obviously our 3X10 table is going to match 0% of the time if col1 is [1,2,3] and col2 contains values from 1000 to 1100. The MCVs would be particularly valuable here; if the same MCV appears in both columns, we can multiply the probabilities. To me, this seems just like estimating on a foreign table match, only simpler. Of course, a coefficient of corellation would make it even more accurate, but even without one we can arrive at much better estimates than defaultsel. > As a concrete > example, applying the join selectivity logic would certainly give a > completely wrong answer for X = X, unless there were only one value > occurring in the column. Yeah, I think we'll eventually need to special-case that one. In the particular case I ran across, though, using column match estimation would have still yielded a better result than defaultsel. -- Josh Berkus PostgreSQL Experts Inc. http://pgexperts.com -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance