X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id BFC42D1C4B9 for ; Tue, 29 Jul 2003 14:09:13 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 70204-07 for ; Tue, 29 Jul 2003 11:08:18 -0300 (ADT) Received: from sss.pgh.pa.us (unknown [192.204.191.242]) by svr1.postgresql.org (Postfix) with ESMTP id 74ABBD1C4B7 for ; Tue, 29 Jul 2003 11:09:11 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.12.9/8.12.9) with ESMTP id h6TE9FQ3021658; Tue, 29 Jul 2003 10:09:15 -0400 (EDT) To: Rajesh Kumar Mallah Cc: pgsql-performance@postgresql.org Subject: Re: Why performance improvement on converting subselect to a function ? In-reply-to: <200307291114.29567.mallah@trade-india.com> References: <200307291114.29567.mallah@trade-india.com> Comments: In-reply-to Rajesh Kumar Mallah message dated "Tue, 29 Jul 2003 11:14:29 +0530" Date: Tue, 29 Jul 2003 10:09:15 -0400 Message-ID: <21657.1059487755@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200307/462 X-Sequence-Number: 2755 Rajesh Kumar Mallah writes: > explain analyze SELECT company_id , (SELECT edition FROM ONLY > public.branding_master b WHERE old_company_id = a.company_id OR company_id = > a.company_id ORDER BY b.company_id DESC LIMIT 1) from public.branding_master > a limit 50; > Total runtime: 19429.76 msec > CREATE FUNCTION most_recent_edition (integer) returns integer AS 'SELECT > edition::integer FROM ONLY public.branding_master b WHERE old_company_id = $1 > OR company_id = $1 ORDER BY b.company_id DESC LIMIT 1 ' language 'sql'; > tradein_clients=# explain analyze SELECT company_id , > most_recent_edition(company_id) from public.branding_master limit 50; > Total runtime: 3969.52 msec Odd. Apparently the planner is picking a better plan in the function context than in the subselect context --- which is strange since it ought to have less information. AFAIK the only way to see the plan generated for a SQL function's query is like this: regression=# create function foo(int) returns int as regression-# 'select unique1 from tenk1 where unique1 = $1' language sql; CREATE FUNCTION regression=# set debug_print_plan TO 1; SET regression=# set client_min_messages TO debug; SET regression=# select foo(55); DEBUG: plan: DETAIL: {RESULT :startup_cost 0.00 :total_cost 0.01 :plan_rows 1 :plan_width 0 :targetlist ({TARGETENTRY :resdom {RESDOM :resno 1 :restype 23 :restypmod -1 :resname foo :ressortgroupref 0 :resorigtbl 0 :resorigcol 0 :resjunk false} :expr {FUNCEXPR :funcid 706101 :funcresulttype 23 :funcretset false ... (etc etc) Would you do that and send it along? I'm curious ... > But i feel it can be lot more faster , can anyone suggest me something > to try. Create an index on old_company_id, perhaps. regards, tom lane