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 3758E528CB for ; Tue, 19 Jul 2005 06:49:39 -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 79689-08 for ; Tue, 19 Jul 2005 09:49:37 +0000 (GMT) Received: from mail.metronet.co.uk (mail.metronet.co.uk [213.162.97.75]) by svr1.postgresql.org (Postfix) with ESMTP id C22F6528C6 for ; Tue, 19 Jul 2005 06:49:36 -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 1CDE8417853; Tue, 19 Jul 2005 10:49:32 +0100 (BST) Received: from localhost (localhost.localdomain [127.0.0.1]) by mainbox.archonet.com (Postfix) with ESMTP id 2846A15EE6; Tue, 19 Jul 2005 10:39:11 +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 03747-03; Tue, 19 Jul 2005 10:39:07 +0100 (BST) Received: from [192.168.1.17] (client17.office.archonet.com [192.168.1.17]) by mainbox.archonet.com (Postfix) with ESMTP id 9E5A015EDA; Tue, 19 Jul 2005 10:39:07 +0100 (BST) Message-ID: <42DCCA3B.8070100@archonet.com> Date: Tue, 19 Jul 2005 10:39:07 +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: pgsql-performance@postgresql.org Subject: Re: Insert performance (OT?) References: 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/276 X-Sequence-Number: 13517 Yves Vindevogel wrote: > Hi, > > Suppose I have a table with 4 fields (f1, f2, f3, f4) > I define 2 unique indexes u1 (f1, f2, f3) and u2 (f1, f2, f4) > > I have 3 records > A, B, C, D (this will be inserted) > A, B, C, E (this will pass u2, but not u1, thus not inserted) > A, B, F, D (this will pass u1, but not u2, thus not inserted) Are you saying you want to know whether they will be inserted before you try to do so? > Now, for performance ... > > I have tables like this with 500.000 records where there's a new upload > of approx. 20.000 records. > It is only now that we say index u2 to be necessary. So, until now, I > did something like insert into ... select f1, f2, f2, max(f4) group by > f1, f2, f3 > That is ok ... and also logically ok because of the data definition I'm confused here - assuming you meant "select f1,f2,f3", then I don't see how you guarantee the row doesn't alredy exist. > I cannot do this with 2 group by's. I tried this on paper and I'm not > succeeding. I don't see how you can have two group-by's, or what that would mean if you did. > 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; Are you saying that deleting these rows and then inserting takes too long? -- Richard Huxton Archonet Ltd