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 71EAC52E88 for ; Tue, 26 Jul 2005 14:20:32 -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 92700-06 for ; Tue, 26 Jul 2005 17:20:26 +0000 (GMT) Received: from sccmmhc92.asp.att.net (sccmmhc92.asp.att.net [204.127.203.212]) by svr1.postgresql.org (Postfix) with ESMTP id B112E52EA2 for ; Tue, 26 Jul 2005 14:20:24 -0300 (ADT) Received: from juju.arbash-meinel.com ([12.214.18.81]) by sccmmhc92.asp.att.net (sccmmhc92) with ESMTP id <20050726172024m9200g48k4e>; Tue, 26 Jul 2005 17:20:24 +0000 Received: by juju.arbash-meinel.com (Postfix, from userid 505) id 4176156011; Tue, 26 Jul 2005 11:56:26 -0500 (CDT) Received: from [192.168.1.11] (liliana.arbash-meinel.com [192.168.1.11]) by juju.arbash-meinel.com (Postfix) with ESMTP id 2307E55FB5; Tue, 26 Jul 2005 11:56:15 -0500 (CDT) Message-ID: <42E66B30.3030108@arbash-meinel.com> Date: Tue, 26 Jul 2005 11:56:16 -0500 From: John A Meinel User-Agent: Mozilla Thunderbird 1.0.2 (Windows/20050317) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Dan Harris Cc: pgsql-performance@postgresql.org Subject: Re: faster INSERT with possible pre-existing row? References: In-Reply-To: X-Enigmail-Version: 0.92.0.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="------------enig9C21B405ABDA157CADDE32ED" X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.045 tagged_above=0 required=5 tests=AWL, FORGED_RCVD_HELO X-Spam-Level: X-Archive-Number: 200507/390 X-Sequence-Number: 13631 This is an OpenPGP/MIME signed message (RFC 2440 and 3156) --------------enig9C21B405ABDA157CADDE32ED Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Dan Harris wrote: > I am working on a process that will be inserting tens of million rows > and need this to be as quick as possible. > > The catch is that for each row I could potentially insert, I need to > look and see if the relationship is already there to prevent multiple > entries. Currently I am doing a SELECT before doing the INSERT, but I > recognize the speed penalty in doing to operations. I wonder if there > is some way I can say "insert this record, only if it doesn't exist > already". To see if it exists, I would need to compare 3 fields > instead of just enforcing a primary key. > > Even if this could be a small increase per record, even a few percent > faster compounded over the whole load could be a significant reduction. > > Thanks for any ideas you might have. > > -Dan > You could insert all of your data into a temporary table, and then do: INSERT INTO final_table SELECT * FROM temp_table WHERE NOT EXISTS (SELECT info FROM final_table WHERE id=id, path=path, y=y); Or you could load it into the temporary table, and then: DELETE FROM temp_table WHERE EXISTS (SELECT FROM final_table WHERE id...); And then do a plain INSERT INTO. I can't say what the specific performance increases would be, but temp_table could certainly be an actual TEMP table (meaning it only exists during the connection), and you could easily do a COPY into that table to load it up quickly, without having to check any constraints. Just a thought, John =:-> --------------enig9C21B405ABDA157CADDE32ED Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.1 (Cygwin) Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org iD8DBQFC5mswJdeBCYSNAAMRAupDAKCrP6dp0q0HGvri2TlUZfeg/zZENwCcDUoS OuTezkceW29aUAbY/2rAe9k= =21Zo -----END PGP SIGNATURE----- --------------enig9C21B405ABDA157CADDE32ED--