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 C57DF52914 for ; Tue, 19 Jul 2005 08:00: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 95493-02 for ; Tue, 19 Jul 2005 11:00:41 +0000 (GMT) Received: from mail.metronet.co.uk (mail.metronet.co.uk [213.162.97.75]) by svr1.postgresql.org (Postfix) with ESMTP id 4564E5293E for ; Tue, 19 Jul 2005 08:00:37 -0300 (ADT) Received: from mainbox.archonet.com (84-51-143-99.archon037.adsl.metronet.co.uk [84.51.143.99]) by smtp.metronet.co.uk (MetroNet Mail) with ESMTP id 517294183D5; Tue, 19 Jul 2005 12:00:20 +0100 (BST) Received: from localhost (localhost.localdomain [127.0.0.1]) by mainbox.archonet.com (Postfix) with ESMTP id 9600F15EE6; Tue, 19 Jul 2005 11:51:55 +0100 (BST) Received: from mainbox.archonet.com ([127.0.0.1]) by localhost (mainbox [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 04342-02; Tue, 19 Jul 2005 11:51:52 +0100 (BST) Received: from [192.168.1.17] (client17.office.archonet.com [192.168.1.17]) by mainbox.archonet.com (Postfix) with ESMTP id 1CD3815EDA; Tue, 19 Jul 2005 11:51:52 +0100 (BST) Message-ID: <42DCDB47.3060009@archonet.com> Date: Tue, 19 Jul 2005 11:51:51 +0100 From: Richard Huxton User-Agent: Mozilla Thunderbird 1.0.2 (X11/20050317) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Yves Vindevogel Cc: 'Postgresql Performance' Subject: Re: Insert performance (OT?) References: <42DCCA3B.8070100@archonet.com> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.056 tagged_above=0 required=5 tests=AWL, FORGED_RCVD_HELO X-Spam-Level: X-Archive-Number: 200507/278 X-Sequence-Number: 13519 Yves Vindevogel wrote: >>> So, I must use a function that will check against u1 and u2, and then >>> insert if it is ok. >>> I know that such a function is way slower that my insert query. >> >> So - you have a table, called something like "upload" with 20,000 rows >> and you'd like to know whether it is safe to insert them. Well, it's >> easy enough to identify which ones are duplicates. >> >> SELECT * FROM upload JOIN main_table ON u1=f1 AND u2=f2 AND u3=f3; >> SELECT * FROM upload JOIN main_table ON u1=f1 AND u2=f2 AND u3=f4; >> > That is a good idea. I can delete the ones that would fail my first > unique index this way, and then delete the ones that would fail my > second unique index and then upload them. > Hmm, why did I not think of that myself. I've spent a lot of time moving data from one system to another, usually having to clean it in the process. At 9pm on a Friday, you decide that on the next job you'll find an efficient way to do it :-) >> Are you saying that deleting these rows and then inserting takes too >> long? >> > This goes very fast, but not with a function that checks each record one > by one. You could get away with one query if you converted them to left-joins: INSERT INTO ... SELECT * FROM upload LEFT JOIN ... WHERE f3 IS NULL UNION SELECT * FROM upload LEFT JOIN ... WHERE f4 IS NULL The UNION will remove duplicates for you, but this might turn out to be slower than two separate queries. -- Richard Huxton Archonet Ltd