X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id ECCB09FA2DB for ; Mon, 5 Jun 2006 12:04:51 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 28161-02 for ; Mon, 5 Jun 2006 12:04:43 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from noel.decibel.org (noel.decibel.org [67.100.216.10]) by postgresql.org (Postfix) with ESMTP id B50B39FA5D2 for ; Mon, 5 Jun 2006 12:04:42 -0300 (ADT) Received: by noel.decibel.org (Postfix, from userid 1001) id A2B4156452; Mon, 5 Jun 2006 10:04:26 -0500 (CDT) Received: (hashcash-sendmail, from uid 1001); Mon, 5 Jun 2006 10:04:25 -0500 Date: Mon, 5 Jun 2006 10:04:25 -0500 From: "Jim C. Nasby" To: Michael Artz Cc: pgsql-performance@postgresql.org Subject: Re: Bulk loading/merging Message-ID: <20060605150425.GS53487@pervasive.com> References: <20060531010922.GC59464@pervasive.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Operating-System: FreeBSD 6.0-RELEASE-p4 amd64 X-Distributed: Join the Effort! http://www.distributed.net User-Agent: Mutt/1.5.11 X-Hashcash: 1:20:060605:mlartz@gmail.com::Eiw9lbbOWECRegIu:02jTV X-Hashcash: 1:20:060605:pgsql-performance@postgresql.org::gft6IQt79wfQaxEo:00000 0000000000000000000000009KmO X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200606/37 X-Sequence-Number: 19394 On Thu, Jun 01, 2006 at 02:04:46PM -0400, Michael Artz wrote: > On 5/30/06, Jim C. Nasby wrote: > > >Your best bet is to do this as a single, bulk operation if possible. > >That way you can simply do an UPDATE ... WHERE EXISTS followed by an > >INSERT ... SELECT ... WHERE NOT EXISTS. > > > > hmm, I don't quite understand what you are saying and I think my > basic misunderstanding is how to use the UPDATE ... WHERE EXISTS to merge > data in bulk. Assuming that I bulk COPYed the data into a temporary > table, I'd need to issue an UPDATE for each row in the newly created table, > right? > > For example, for a slightly different key,count schema: > > CREATE TABLE kc (key integer, count integer); > > and wanting to merge the following data by just updating the count for a > given key to the equivalent of OLD.count + NEW.count: > > 1,10 > 2,15 > 3,45 > 1,30 > > How would I go about using UPDATE ... WHERE EXISTS to update the "master" kc > table from a (temporary) table loaded with the above data? CREATE TEMP TABLE moo () LIKE kc; COPY ... moo; BEGIN; UPDATE kc SET count=kc.count + moo.count FROM moo WHERE moo.key = kc.key ; INSERT INTO kc(key, count) SELECT key, count FROM moo WHERE NOT EXISTS ( SELECT 1 FROM kc WHERE kc.key = moo.key ) ; COMMIT; -- Jim C. Nasby, Sr. Engineering Consultant jnasby@pervasive.com Pervasive Software http://pervasive.com work: 512-231-6117 vcard: http://jim.nasby.net/pervasive.vcf cell: 512-569-9461