Re: alter table TBL add constraint TBL_FK foreign key

From: Ron Johnson <ron(dot)l(dot)johnson(at)cox(dot)net>
To: PgSQL Performance ML <pgsql-performance(at)postgresql(dot)org>
Subject: Re: alter table TBL add constraint TBL_FK foreign key
Date: 2003-01-01 08:14:34
Message-ID: 1041408874.16580.39.camel@haggis
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

On Tue, 2002-12-31 at 21:32, Minghann Ho wrote:
> Hi all,
>
> 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 !!!
>
> I've read in the archives that some people suffered slow performance of this
> problem in Aug 2000, but there was no further information about the solution.
>
> Please anyone who has experience in this issues can give me some hint.

Silly question: Are T2 & T3 compound-key indexed on the relevant foreign
key fields (in the exact order that they are mentioned in the ADD
CONSTRAINT command)? Otherwise, for each record in T1, it is scanning
T2 1.5M times (9E12 record reads!), with a similar formula for T1->T3.

--
+------------------------------------------------------------+
| Ron Johnson, Jr. mailto:ron(dot)l(dot)johnson(at)cox(dot)net |
| Jefferson, LA USA http://members.cox.net/ron.l.johnson |
| |
| "Basically, I got on the plane with a bomb. Basically, I |
| tried to ignite it. Basically, yeah, I intended to damage |
| the plane." |
| RICHARD REID, who tried to blow up American Airlines |
| Flight 63 |
+------------------------------------------------------------+

In response to

Browse pgsql-performance by date

  From Date Subject
Next Message Tomasz Myrta 2003-01-01 09:59:06 Re: 7.3.1 New install, large queries are slow
Previous Message Stephan Szabo 2003-01-01 03:38:54 Re: alter table TBL add constraint TBL_FK foreign key ...