Re: advice

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: josh(at)agliodbs(dot)com
Cc: Andrei Ivanov <andrei(dot)ivanov(at)ines(dot)ro>, pgsql-novice(at)postgresql(dot)org
Subject: Re: advice
Date: 2003-10-22 22:48:30
Message-ID: 24667.1066862910@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Josh Berkus <josh(at)agliodbs(dot)com> writes:
>> I'm planning to something like
>> INSERT INTO first_table
>> SELECT * FROM second_table s WHERE
>> NOT EXISTS (SELECT 1 FROM first_table WHERE id = s.id)

> This is the way to do it, athough I'd fully qualify both "id" fields in the
> subquery and use an alias to avoid confusion, e.g.
> NOT EXISTS (SELECT 1 FROM first_table f2 WHERE f2.id = s.id)

Since the tables have identical column sets, you could also think about

INSERT INTO first_table
(SELECT * FROM second_table EXCEPT SELECT * FROM first_table)

which might or might not run considerably faster. It'd be worth trying
I think.

regards, tom lane

In response to

  • Re: advice at 2003-10-22 20:27:25 from Josh Berkus

Browse pgsql-novice by date

  From Date Subject
Next Message Michael Glaesmann 2003-10-23 00:32:53 Re: Functional index problems. (Was: Many joins: monthly summaries S-L--O--W)
Previous Message Peter Eisentraut 2003-10-22 22:01:57 Re: Expressional Indexes