Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 34A00475E24 for ; Tue, 31 Dec 2002 22:38:50 -0500 (EST) Received: from megazone.bigpanda.com (megazone.bigpanda.com [63.150.15.178]) by postgresql.org (Postfix) with ESMTP id C0BBF475B47 for ; Tue, 31 Dec 2002 22:38:49 -0500 (EST) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id 35D42D610; Tue, 31 Dec 2002 19:38:54 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id 2B8395C02; Tue, 31 Dec 2002 19:38:54 -0800 (PST) Date: Tue, 31 Dec 2002 19:38:54 -0800 (PST) From: Stephan Szabo To: Minghann Ho Cc: Subject: Re: alter table TBL add constraint TBL_FK foreign key ... In-Reply-To: <200301011632.10433.Minghann.Ho@mcs.vuw.ac.nz> Message-ID: <20021231193351.U68640-100000@megazone23.bigpanda.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200212/231 X-Sequence-Number: 647 On Wed, 1 Jan 2003, Minghann Ho wrote: > I've experienced very slow performance to add foreign key constraints using > ALTER TABLE ADD CONSTRAINT FOREIGN KEY ... > > After using COPY ... FROM to load the base tables, I started to build the > referential integrity between tables. > I have 3 tables: T1 (6 million records), T2 (1.5 million records) and T3 (0.8 > million records). > One of the RI - foreign key (T1 -> T2) constraint took about 70 hrs to build. > The other RI - foreign key (T1 -> T3) constraint took about 200 hrs and yet > completed!! (compound foreign key) > > I tried to use small subset of the tables of T2 and T3 to do the testing. > An estimation show that it need about 960 hrs to build the RI - foreign key > constraints on table T1 -> T3 !!! It's running the constraint check for each row in the foreign key table. Rather than using a call to the function and a select for each row, it could probably be done in a single select with a not exists subselect, but that hasn't been done yet. There's also been talk about allowing some mechanism to allow the avoidance of the create time check, but I don't think any concensus was reached.