pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Linux Guru <linux.binary@gmail.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: pgsql-performance@postgresql.org
Subject: Re: Update with Subquery Performance
Date: Wed, 13 Feb 2008 15:59:26 +0500
Message-ID: <3caa866c0802130259y26d54baeh457f65d9adc71482@mail.gmail.com> (raw)
In-Reply-To: <27294.1202833085@sss.pgh.pa.us>
References: <3caa866c0802110306rcb5e72dyd264ff330c61f8ae@mail.gmail.com>
	<2982.1202749162@sss.pgh.pa.us>
	<3caa866c0802120032r7b7eb672s98d38f1351622424@mail.gmail.com>
	<27294.1202833085@sss.pgh.pa.us>

yes, I also thought of this method and tested it before I got your mail and
this solution seems workable.

Thanks for the help

On Feb 12, 2008 9:18 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:

> "Linux Guru" <linux.binary@gmail.com> writes:
> > Analyzing did not help, here is the out of EXPLAIN ANALYZE of update
> query
> > "Seq Scan on dummy  (cost=0.00..56739774.24 rows=23441 width=275)
> (actual
> > time=18.927..577929.014 rows=22712 loops=1)"
> > "  SubPlan"
> > "    ->  Aggregate  (cost=2420.41..2420.43 rows=1 width=19) (actual
> time=
> > 25.423..25.425 rows=1 loops=22712)"
> > "          ->  Seq Scan on dummy "temp"  (cost=0.00..2416.01 rows=586
> > width=19) (actual time=0.049..17.834 rows=2414 loops=22712)"
> > "                Filter: ((product)::text = ($0)::text)"
> > "Total runtime: 578968.885 ms"
>
> Yeah, that's just not going to be fast.  An index on the product column
> might help a bit, but the real issue is that you're repetitively
> calculating the same aggregates.  I think you need a separate temp
> table, along the lines of
>
> create temp table dummy_agg as
>  select product,
>         (case when sum(pd) <> 0 then sum(gd)/sum(pd)*100 else 0 end) as s
>  from dummy
>  group by product;
>
> create index dummy_agg_i on dummy_agg(product); -- optional
>
> update dummy
>  set gp= (select s from dummy_agg where dummy_agg.product = dummy.product
> );
>
> The index would only be needed if you expect a lot of rows (lot of
> different product values).
>
>                        regards, tom lane
>

view thread (7+ messages)

Message-ID: <3caa866c0802130259y26d54baeh457f65d9adc71482@mail.gmail.com>
Permalink:  ../3caa866c0802130259y26d54baeh457f65d9adc71482@mail.gmail.com/
Also on:    postgresql.org/message-id/3caa866c0802130259y26d54baeh457f65d9adc71482@mail.gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-performance@postgresql.org
  Cc: linux.binary@gmail.com, tgl@sss.pgh.pa.us
  Subject: Re: Update with Subquery Performance
  In-Reply-To: <3caa866c0802130259y26d54baeh457f65d9adc71482@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox