X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 074BD9DCBD3 for ; Thu, 2 Mar 2006 13:04:23 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 12108-05 for ; Thu, 2 Mar 2006 13:04:21 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from tigger.fuhr.org (tigger.fuhr.org [63.214.45.158]) by postgresql.org (Postfix) with ESMTP id 9C3259DCBCA for ; Thu, 2 Mar 2006 13:04:20 -0400 (AST) Received: from winnie.fuhr.org (winnie.fuhr.org [10.1.0.1]) by tigger.fuhr.org (8.13.3/8.13.3) with ESMTP id k22H4FI8079487 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Thu, 2 Mar 2006 10:04:17 -0700 (MST) (envelope-from mfuhr@winnie.fuhr.org) Received: from winnie.fuhr.org (localhost [127.0.0.1]) by winnie.fuhr.org (8.13.4/8.13.4) with ESMTP id k22H4EL4007714; Thu, 2 Mar 2006 10:04:14 -0700 (MST) (envelope-from mfuhr@winnie.fuhr.org) Received: (from mfuhr@localhost) by winnie.fuhr.org (8.13.4/8.13.4/Submit) id k22H4E1J007713; Thu, 2 Mar 2006 10:04:14 -0700 (MST) (envelope-from mfuhr) Date: Thu, 2 Mar 2006 10:04:14 -0700 From: Michael Fuhr To: "Chaz." Cc: pgsql-sql@postgresql.org Subject: Re: Question about Sql SELECT and optimizer Message-ID: <20060302170414.GA7634@winnie.fuhr.org> References: <44071B14.4060906@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <44071B14.4060906@gmail.com> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.122 required=5 tests=[AWL=0.122] X-Spam-Score: 0.122 X-Spam-Level: X-Archive-Number: 200603/51 X-Sequence-Number: 24397 On Thu, Mar 02, 2006 at 11:19:32AM -0500, Chaz. wrote: > I am trying to understand something I have seen happen. I had a select > that looked like: > > select f(A) from A, B, C where g(A) > > Where f(A) is the select that only depends on table A; > g(A) is the where part that only depends on table A. > > What I saw happen was the optimizer will waste a lot of time (seconds!) > bringing in table B and C. I was wondering why doesn't the optimizer > drop references to tables B and C since they aren't used any where? The above query does a cross join. Even though you're not using values from B and C they're still contributing rows to the result set. test=> SELECT * FROM a; aid ----- a1 a2 (2 rows) test=> SELECT * FROM b; bid ----- b1 b2 (2 rows) test=> SELECT * FROM c; cid ----- c1 c2 (2 rows) test=> SELECT a.*, b.*, c.* FROM a, b, c WHERE a.aid = 'a1'; aid | bid | cid -----+-----+----- a1 | b1 | c1 a1 | b2 | c1 a1 | b1 | c2 a1 | b2 | c2 (4 rows) test=> SELECT a.*, b.* FROM a, b, c WHERE a.aid = 'a1'; aid | bid -----+----- a1 | b1 a1 | b2 a1 | b1 a1 | b2 (4 rows) test=> SELECT a.* FROM a, b, c WHERE a.aid = 'a1'; aid ----- a1 a1 a1 a1 (4 rows) -- Michael Fuhr