From: Jacques Caron <jc@directinfos.com>
To: Bricklen Anderson <BAnderson@PresiNET.com>
Cc: pgsql-performance@postgresql.org
Subject: Re: ETL optimization
Date: Thu, 23 Jun 2005 23:56:27 +0200
Message-ID: <6.2.0.14.0.20050623234711.053c4d48@pop.interactivemediafactory.net> (raw)
In-Reply-To: <42BB0F9C.2050207@PresiNET.com>
References: <42BB0F9C.2050207@PresiNET.com>
Hi,
At 21:38 23/06/2005, Bricklen Anderson wrote:
>Situation:
>I'm trying to optimize an ETL process with many upserts (~100k aggregated
>rows)
>(no duplicates allowed). The source (table t2) table holds around 14 million
>rows, and I'm grabbing them 100,000 rows at a time from t2, resulting in about
>100,000 distinct rows in the destination table (t1).
>
>
>What I've tried:
>
>i. FOR EXECUTE LOOP over my result set (aggregated results, 100k-ish
>rows), and
>try an update first, check the ROW_COUNT, if 0, then do an insert.
>...
>run time: approx. 25 mins
>
>
>ii. in a function (pseudo code), (table name is dynamic):
>...
>up_stm :=
>'UPDATE '||t1||' SET x=t2.x
>FROM (select sum(x),a,b,c
> from t2
> group by a,b,c) as t2
>WHERE '||t1||'.a=t2.a AND '||t1||'.b=t2.b AND '||t1||'.c=t3.c';
>
>EXECUTE up_stm;
>
>ins_stm :=
>'INSERT INTO '||t1||' (x,a,b,c) select x,a,b,c
>FROM (select sum(x) as x,a,b,c from t2 group by a,b,c) as t2
>WHERE NOT EXISTS
> (select true from '||t1||'
> where '||t1||'.a=t2.a
> and '||t1||'.b=t2.b
> and '||t1||'.c=t2.c
> limit 1)';
>
>EXECUTE ins_stm;
>...
I have a similar situation, and the solution I use (though I haven't really
tested many different situations):
- have a trigger ON INSERT which does:
UPDATE set whatever_value=NEW.whatever_value,... WHERE
whatever_key=NEW.whatever.key AND...
IF FOUND THEN
RETURN NULL;
ELSE
RETURN NEW;
END IF;
- use COPY
For optimal performance, a different trigger function is created for each
table, which allows the query plan of the UPDATE to be cached.
Let us know how that works out for you and if you find a better solution!
Jacques.
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 from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-performance@postgresql.org
Cc: jc@directinfos.com, BAnderson@PresiNET.com
Subject: Re: ETL optimization
In-Reply-To: <6.2.0.14.0.20050623234711.053c4d48@pop.interactivemediafactory.net>
References: <42BB0F9C.2050207@PresiNET.com> <6.2.0.14.0.20050623234711.053c4d48@pop.interactivemediafactory.net>
* 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