X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 2EDED52995 for ; Thu, 23 Jun 2005 18:56:45 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 58265-06 for ; Thu, 23 Jun 2005 21:56:44 +0000 (GMT) Received: from mars.interactivemediafactory.net (mars.imfeurope.net [194.2.222.161]) by svr1.postgresql.org (Postfix) with ESMTP id 8292D52983 for ; Thu, 23 Jun 2005 18:56:41 -0300 (ADT) Received: from JC-8600.directinfos.com (localhost [127.0.0.1]) by mars.interactivemediafactory.net (8.12.11/8.12.11) with ESMTP id j5NLuf0Y095859; Thu, 23 Jun 2005 23:56:42 +0200 (CEST) (envelope-from jc@directinfos.com) Message-Id: <6.2.0.14.0.20050623234711.053c4d48@pop.interactivemediafactory.net> X-Mailer: QUALCOMM Windows Eudora Version 6.2.0.14 Date: Thu, 23 Jun 2005 23:56:27 +0200 To: Bricklen Anderson From: Jacques Caron Subject: Re: ETL optimization Cc: pgsql-performance@postgresql.org In-Reply-To: <42BB0F9C.2050207@PresiNET.com> References: <42BB0F9C.2050207@PresiNET.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii"; format=flowed X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.05 tagged_above=0 required=5 tests=FORGED_RCVD_HELO X-Spam-Level: X-Archive-Number: 200506/466 X-Sequence-Number: 13103 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.