X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 5BEBBDAF91 for ; Wed, 26 Oct 2005 20:18:36 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 40812-01 for ; Wed, 26 Oct 2005 23:18:32 +0000 (GMT) Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by svr1.postgresql.org (Postfix) with ESMTP id 46E9ADAF7B for ; Wed, 26 Oct 2005 20:18:34 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.1/8.13.1) with ESMTP id j9QNIbt6012438; Wed, 26 Oct 2005 19:18:37 -0400 (EDT) To: "Edward Di Geronimo Jr." Cc: pgsql-performance@postgresql.org Subject: Re: Performance issues with custom functions In-reply-to: <435FE820.4070701@xtracards.com> References: <435FE820.4070701@xtracards.com> Comments: In-reply-to "Edward Di Geronimo Jr." message dated "Wed, 26 Oct 2005 16:33:36 -0400" Date: Wed, 26 Oct 2005 19:18:37 -0400 Message-ID: <12437.1130368717@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.006 required=5 tests=[AWL=0.006] X-Spam-Level: X-Archive-Number: 200510/401 X-Sequence-Number: 15169 "Edward Di Geronimo Jr." writes: > ... I'd like to know exactly what causes > the bottleneck in the original query, and if there are other approaches > to solving the issue in case I need them in future queries. This is fairly hard to read ... it would help a lot if you had shown the view definitions that the query relies on, so that we could match up the plan elements with the query a bit better. However, I'm thinking the problem is with this IN clause: > where pl.network_id in (select ns.network_id > from development.network_state ns > where ns.from_date < current_time > and (ns.thru_date > current_time or > ns.thru_date is null) > and (ns.state_cd = pl.state_cd or ns.state_cd='') > ) Because the sub-SELECT references pl.state_cd (an outer variable reference), there's no chance of optimizing this into a join-style IN. So the sub-SELECT has to be re-executed for each row of the outer query. BTW, it's not apparent to me that your "flattened" query gives the same answers as the original. What if a pl row can join to more than one row of the ns output? regards, tom lane