Received: from localhost (unknown [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id BCE3B2E326A for ; Mon, 11 Feb 2008 17:29:45 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 14344-07 for ; Mon, 11 Feb 2008 17:29:37 -0400 (AST) Received: from wa-out-1112.google.com (wa-out-1112.google.com [209.85.146.176]) by postgresql.org (Postfix) with ESMTP id EF3842E325B for ; Mon, 11 Feb 2008 17:29:41 -0400 (AST) Received: by wa-out-1112.google.com with SMTP id m38so2632733waf.5 for ; Mon, 11 Feb 2008 13:29:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; bh=CKoQaY1oaYG4zjFjq3Yyx5i0B05HmyaQ9yFJDaQ9ETE=; b=Y75JLkTVTmvKwkQEudv/A57jfcqiruGQ5fmSlJ/WiWXQ945p8Xp42L92qFcA573em+L11VnSyC1s4EvDL7NxSQhtppLQwTWpu/ZIRm7SQM+5z/zDQjpZvc1RYeCSD7XLjgpdcNRq3oQ1dVFNatwcXoMZOmNDsIZT6vE3n0S2o2E= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=SQHeETZ+zF+4c388BBhVFqTvkaCJR4Z8w2KEAshGsZkPddD6SxBTC8RhuTMojSe1C/4TtXd5zV9fJZaYs/++7Vl3KXWDQfZHTpPaZs6cXMFwg388/9oA/SjjiucvAv0f+iWBpgc81dqb6QcR/7XDG64iJSHdfO3l1IVAKDDhjl0= Received: by 10.115.17.11 with SMTP id u11mr505324wai.149.1202765380805; Mon, 11 Feb 2008 13:29:40 -0800 (PST) Received: by 10.114.131.4 with HTTP; Mon, 11 Feb 2008 13:29:40 -0800 (PST) Message-ID: Date: Mon, 11 Feb 2008 15:29:40 -0600 From: "Scott Marlowe" To: "Linux Guru" Subject: Re: Update with Subquery Performance Cc: pgsql-performance@postgresql.org In-Reply-To: <3caa866c0802110306rcb5e72dyd264ff330c61f8ae@mail.gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <3caa866c0802110306rcb5e72dyd264ff330c61f8ae@mail.gmail.com> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200802/102 X-Sequence-Number: 28644 On Feb 11, 2008 5:06 AM, Linux Guru wrote: > We have a large datawarehouse stored in postgres and temp tables are created > based on user query. The process of temp table creation involves selecting > data from main fact table, this includes several select and update > statements and one of the following update statement is having performance > issues. > > The newly temp table created for this scenario contains 22712 rows. Here is > the query > > alter table dummy add column gp numeric(40,15); > update dummy set gp=(select ( > case when sum(temp.pd) <> 0 then sum(temp.gd)/sum(temp.pd)*100 else 0 end ) > from dummy as temp > where temp.product=dummy.product) Is this supposed to be updating every single row with one value? Cause I'm guessing it's running that sub select over and over instead of one time. I'm guessing that with more work_mem the planner might use a more efficient plan. Try adding analyze; set work_mem = 128000; between the alter and update and see if that helps. Also, as Tom said, post explain analyze output of the statement. > > Now this query basically updates a table using values within itself in the > subquery but it takes tooooo much time i.e. approx 5 mins. The whole temp > table creation process is stucked in this query (there are 4 additional such > updates with same problem). Index creation is useless here since its only a > one time process. > > Here is the strip down version (the part making performance issue) of above > query i.e. only select statement > ------------------------------- > select (case when sum(temp.pd) <> 0 then sum(temp.gd)/sum(temp.pd)*100 else > 0 end ) from dummy as temp, dummy as temp2 > where temp.product=temp2.product group by temp.product > > "HashAggregate (cost=1652480.98..1652481.96 rows=39 width=39)" > " -> Hash Join (cost=1636.07..939023.13 rows=71345785 width=39)" > " Hash Cond: (("temp".product)::text = (temp2.product)::text)" > " -> Seq Scan on dummy "temp" (cost=0.00..1311.03 rows=26003 > width=39)" > " -> Hash (cost=1311.03..1311.03 rows=26003 width=21)" > " -> Seq Scan on dummy temp2 (cost=0.00..1311.03 rows=26003 > width=21)" > ------------------------------- > > > Whats the solution of this problem, or any alternate way to write this > query? > > >