X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 82C369DC9F7 for ; Wed, 22 Mar 2006 12:09:30 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 82414-09 for ; Wed, 22 Mar 2006 12:09:28 -0400 (AST) 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 E64FB9DC992 for ; Wed, 22 Mar 2006 12:09:26 -0400 (AST) Received: by noel.decibel.org (Postfix, from userid 1001) id BA35856423; Wed, 22 Mar 2006 10:09:25 -0600 (CST) Received: (hashcash-sendmail, from uid 1001); Wed, 22 Mar 2006 10:09:25 -0600 Date: Wed, 22 Mar 2006 10:09:25 -0600 From: "Jim C. Nasby" To: ashah Cc: pgsql Subject: Re: Massive Inserts Strategies Message-ID: <20060322160924.GJ15742@pervasive.com> References: <200603221032.10207.ashah@opuspro.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <200603221032.10207.ashah@opuspro.com> 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:060322:ashah@opuspro.com::Lte4OFHxN9kYz282:00000000000000000000 0000000000000000000000002hoc X-Hashcash: 1:20:060322:pgsql-performance@postgresql.org::DHNCF0Qrnepi+o0l:00000 00000000000000000000000059zn X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.108 required=5 tests=[AWL=0.108] X-Spam-Score: 0.108 X-Spam-Level: X-Archive-Number: 200603/459 X-Sequence-Number: 17896 Load the files into a temp table and go from there... COPY ... FROM file; UPDATE existing_table SET ... WHERE ...; INSERT INTO existing_table SELECT * FROM temp_table WHERE NOT EXISTS( SELECT * FROM existing_table WHERE ...) On Wed, Mar 22, 2006 at 10:32:10AM -0500, ashah wrote: > I have a database with foreign keys enabled on the schema. I receive different > files, some of them are huge. And I need to load these files in the database > every night. There are several scenerios that I want to design an optimal > solution for - > > 1. One of the file has around 80K records and I have to delete everything from > the table and load this file. The provider never provides a "delta file" so I > dont have a way to identify which records are already present and which are > new. If I dont delete everything and insert fresh, I have to make around 80K > selects to decide if the records exist or not. Now there are lot of tables > that have foreign keys linked with this table so unless I disable the foreign > keys, I cannot really delete anything from this table. What would be a good > practise here? > > 2. Another file that I receive has around 150K records that I need to load in > the database. Now one of the fields is logically a "foreign key" to another > table, and it is linked to the parent table via a database generated unique > ID instead of the actual value. But the file comes with the actual value. So > once again, I have to either drop the foreign key, or make 150K selects to > determine the serial ID so that the foreign key is satisfied. What would be a > good strategy in this scenerio ? > > Please pardon my inexperience with database ! > > Thanks, > Amit > > ---------------------------(end of broadcast)--------------------------- > TIP 4: Have you searched our list archives? > > http://archives.postgresql.org > -- 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