pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Ricardo Vaz Mannrich <rvm_l1@silcom.com.br>
To: pgsql-admin@postgresql.org
Subject: Slow Foreign Key
Date: 22 Mar 2004 15:24:58 -0300
Message-ID: <1079979898.5609.15.camel@paranagua.silcom> (raw)

I'm trying to do a lot of inserts on a detail table, but with foreign
key schema it's too slow.

I made few tests.

1) Master table with 290,000 rows and 4 columns (primary key is SERIAL)
2) Detail table now with 1,300,000 rows and 3 columns (primary key is
SERIAL and I have a column master_id here pointing to a master table
record).
3) If I use:
    ALTER TABLE detail ADD CONSTRAINT master_fk FOREIGN KEY master_id
REFERENCES master

I get 5 INSERTs per second using this:

INSERT INTO master VALUES (DEFAULT, 'Random 1', 'Random 2', 'Random 3');
SELECT CURRVAL('master_master_id_seq');
INSERT INTO detail VALUES (DEFAULT, XX, 999);
(random number of detail records, alternating with a insert into master)

I can insert just 5 rows/second.

4) So, I change it:
    ALTER TABLE detail DROP CONSTRAINT master_fk;

And run INSERTs again.

Now, I have 1000 rows/second (with SELECT checking before every INSERT).

Is FOREIGN KEY so slow like that?  What can I change in PostgreSQL
configuration?

Note that I run some VACUUM and CLUSTER. This let my INSERTs a very
little fast.

My (test) system is:

Linux 2.4.21
PostgreSQL 7.4.1
Pentium IV 1.8Ghz
256MB RAM
HD IDE

Could anyone help me?

-- 
Ricardo Vaz Mannrich <rvm_l1@silcom.com.br>




view thread (5+ messages)  latest in thread

Message-ID: <1079979898.5609.15.camel@paranagua.silcom>
Permalink:  ../1079979898.5609.15.camel@paranagua.silcom/
Also on:    postgresql.org/message-id/1079979898.5609.15.camel@paranagua.silcom

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-admin@postgresql.org
  Cc: rvm_l1@silcom.com.br
  Subject: Re: Slow Foreign Key
  In-Reply-To: <1079979898.5609.15.camel@paranagua.silcom>
  References: <1079979898.5609.15.camel@paranagua.silcom>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox