X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id ED0FD9DC803 for ; Wed, 11 Jan 2006 12:07:28 -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 08850-01 for ; Wed, 11 Jan 2006 12:07:27 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by postgresql.org (Postfix) with ESMTP id 60BE49DC802 for ; Wed, 11 Jan 2006 12:07:26 -0400 (AST) 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 k0BG7Igv013188; Wed, 11 Jan 2006 11:07:18 -0500 (EST) To: Pallav Kalva cc: PGPerformance Subject: Re: Postgres8.0 planner chooses WRONG plan In-reply-to: <43C523EB.3000201@livedatagroup.com> References: <43C523EB.3000201@livedatagroup.com> Comments: In-reply-to Pallav Kalva message dated "Wed, 11 Jan 2006 10:27:39 -0500" Date: Wed, 11 Jan 2006 11:07:18 -0500 Message-ID: <13187.1136995638@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.083 required=5 tests=[AWL=0.083] X-Spam-Score: 0.083 X-Spam-Level: X-Archive-Number: 200601/115 X-Sequence-Number: 16593 Pallav Kalva writes: > I am having problem optimizing this query, Get rid of the un-optimizable function inside the view. You've converted something that should be a join into an unreasonably large number of function calls. > -> Seq Scan on serviceinstance si > (cost=0.00..15831.52 rows=7 width=78) (actual time=174.430..18948.210 > rows=358 loops=1) > Filter: (((subplan) = 'FL'::text) AND > ((subplan) = '099'::text)) > SubPlan > -> Result (cost=0.00..0.01 rows=1 width=0) > (actual time=0.090..0.093 rows=1 loops=3923) > -> Result (cost=0.00..0.01 rows=1 width=0) > (actual time=0.058..0.061 rows=1 loops=265617) The bulk of the cost here is in the second subplan (0.061 * 265617 = 16202.637 msec total runtime), and there's not a darn thing Postgres can do to improve this because the work is all down inside a "black box" function. In fact the planner does not even know that the function call is expensive, else it would have preferred a plan that requires fewer evaluations of the function. The alternative plan you show is *not* faster "because it's an indexscan"; it's faster because get_parametervalue is evaluated fewer times. The useless sub-SELECTs atop the function calls are adding their own little increment of wasted time, too. I'm not sure how bad that is relative to the function calls, but it's certainly not helping. regards, tom lane