Re: COPY with fk's slow

Lists: pgsql-general
From: John Smith <john_smith_45678(at)yahoo(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: COPY with fk's slow
Date: 2003-02-06 07:21:22
Message-ID: 20030206072122.99305.qmail@web40702.mail.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general


I'm playing around with importing 10,000 records into a table with a single index and a single foreign key. COPY takes roughly 10 minutes(!). However, if I drop the foreign key, COPY takes a couple seconds, THEN adding the foreign key back takes another few seconds. Any idea why COPY with a f/k takes so much longer? Any workarounds? Such as lock the table, drop fk, copy, add fk, unlock?

John

---------------------------------
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: John Smith <john_smith_45678(at)yahoo(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: COPY with fk's slow
Date: 2003-02-06 14:31:02
Message-ID: 12040.1044541862@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

John Smith <john_smith_45678(at)yahoo(dot)com> writes:
> I'm playing around with importing 10,000 records into a table with a single index and a single foreign key. COPY takes roughly 10 minutes(!). However, if I drop the foreign key, COPY takes a couple seconds, THEN adding the foreign key back takes another few seconds. Any idea why COPY with a f/k takes so much longer? Any workarounds? Such as lock the table, drop fk, copy, add fk, unlock?

Sounds like you are getting a bad plan for the fk-checking query. Are
the referencing and referenced columns exactly the same datatype? Do
they both have indexes?

BTW, when experimenting with this, keep in mind that the backend caches
query plans for fk queries. You'll typically need to start a fresh
connection after doing something that you hope will improve the plan.

regards, tom lane


From: John Smith <john_smith_45678(at)yahoo(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: COPY with fk's slow
Date: 2003-02-06 20:06:02
Message-ID: 20030206200602.57055.qmail@web40702.mail.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general


Turns out there was a duplicate index (on the id column) in the parent table. Dropped it, readded the f/k and now copy takes 5-10 secs :).
John
Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:John Smith writes:
> I'm playing around with importing 10,000 records into a table with a single index and a single foreign key. COPY takes roughly 10 minutes(!). However, if I drop the foreign key, COPY takes a couple seconds, THEN adding the foreign key back takes another few seconds. Any idea why COPY with a f/k takes so much longer? Any workarounds? Such as lock the table, drop fk, copy, add fk, unlock?

Sounds like you are getting a bad plan for the fk-checking query. Are
the referencing and referenced columns exactly the same datatype? Do
they both have indexes?

BTW, when experimenting with this, keep in mind that the backend caches
query plans for fk queries. You'll typically need to start a fresh
connection after doing something that you hope will improve the plan.

regards, tom lane

---------------------------------
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now