X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 30712D1CCC2 for ; Fri, 9 Apr 2004 13:50:41 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 62545-05 for ; Fri, 9 Apr 2004 13:50:40 -0300 (ADT) Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by svr1.postgresql.org (Postfix) with ESMTP id 538FBD1CA77 for ; Fri, 9 Apr 2004 13:50:39 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.12.11/8.12.11) with ESMTP id i39GoZtX026676; Fri, 9 Apr 2004 12:50:35 -0400 (EDT) To: Ken Geis Cc: Richard Huxton , pgsql-performance@postgresql.org Subject: Re: plan problem In-reply-to: <4073C3DF.2070307@speakeasy.org> References: <40731252.2030002@speakeasy.org> <200404070938.11482.dev@archonet.com> <4073C3DF.2070307@speakeasy.org> Comments: In-reply-to Ken Geis message dated "Wed, 07 Apr 2004 02:03:27 -0700" Date: Fri, 09 Apr 2004 12:50:35 -0400 Message-ID: <26675.1081529435@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200404/120 X-Sequence-Number: 6420 Ken Geis writes: > Does anyone think that the planner issue has merit to address? Can > someone help me figure out what code I would look at? The planner doesn't currently attempt to "drill down" into a sub-select- in-FROM to find statistics about the variables emitted by the sub-select. So it's just falling back to a default estimate of the number of distinct values coming out of the sub-select. The "drilling down" part is not hard; the difficulty comes from trying to figure out whether and how the stats from the underlying column would need to be adjusted for the behavior of the sub-select itself. As an example, the result of (SELECT DISTINCT foo FROM bar) would usually have much different stats from the raw bar.foo column. In your example, the LIMIT clause potentially affects the stats by reducing the number of distinct values. Now in most situations where the sub-select wouldn't change the stats, there's no issue anyway because the planner will flatten the sub-select into the main query. So we really have to figure out the adjustment part before we can think about doing much here. regards, tom lane