Skip site navigation (1) Skip section navigation (2)

Re: Adding a foreign key constraint is extremely slow

From: Bart Samwel <bsamwel(at)liacs(dot)nl>
To: Greg Stark <gsstark(at)mit(dot)edu>
Cc: bsamwel(at)xs4all(dot)nl, pgsql-performance(at)postgresql(dot)org
Subject: Re: Adding a foreign key constraint is extremely slow
Date: 2003-03-26 17:08:51
Message-ID: 3E81DEA3.7060305@liacs.nl (view raw or flat)
Thread:
Lists: pgsql-performance
Greg Stark wrote:
> bsamwel(at)xs4all(dot)nl writes:
> 
> 
>>alter table triples add foreign key(id1) references wwwlog(id);
>>
>>PostgreSQL starts doing heavy work for at least one and a half hour, and I
>>broke it off at that. It is not possible to "explain" a statement like
>>this! Probably what it does is that it will check the foreign key
>>constraint for every field in the table. This will make it completely
>>impossible to load my data, because:
>>
>>(2) I cannot set the foreign key constraints AFTER loading the 0.9 million
>>records because I've got no clue at all how long this operation is going
>>to take.
> 
> 
> Try adding an index on wwwlog(id) so that it can check the constraint without
> doing a full table scan for each value being checked.

AFAIK, because wwwlog(id) is the primary key, this index already exists 
implicitly.  Still, 0.9 million separate index lookups are too slow for 
my purposes, if for example it takes something as low as 1 ms per lookup 
it will still take 900 seconds (= 15 minutes) to complete. As the 
complete adding of the foreign key constraint took about an hour, that 
would suggest an average of 4 ms per lookup, which suggests that the 
index is, in fact, present. :)

Anyway, I've actually waited for the operation to complete. The problem 
is out of my way for now.

Bart


-- 

Leiden Institute of Advanced Computer Science (http://www.liacs.nl)
E-mail: bsamwel(at)liacs(dot)nl    Telephone: +31-71-5277037
Homepage: http://www.liacs.nl/~bsamwel
Opinions stated in this e-mail are mine and not necessarily my employer's.


In response to

pgsql-performance by date

Next:From: Abhishek SharmaDate: 2003-03-27 00:26:01
Subject:
Previous:From: Greg StarkDate: 2003-03-26 14:17:47
Subject: Re: Adding a foreign key constraint is extremely slow

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group