Re: Most efficient way to insert without duplicates

From: Chris Curvey <chris(at)chriscurvey(dot)com>
To: François Beausoleil <francois(at)teksol(dot)info>
Cc: Forums postgresql <pgsql-general(at)postgresql(dot)org>
Subject: Re: Most efficient way to insert without duplicates
Date: 2013-04-17 14:33:21
Message-ID: CADfwSsDL8a7M-4G+=M85nSjvJODkpN+fAcMFeFNW4bsxjZ_gFg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

> INSERT INTO persona_followers(service_id, follower_id, valid_at)
> SELECT service_id, follower_id, NOW()
> FROM (SELECT DISTINCT service_id, follower_id FROM import) AS import
> WHERE NOT EXISTS(SELECT * FROM persona_followers WHERE import.service_id
> = persona_followers.service_id AND import.follower_id =
> persona_followers.follower_id);
>
>
I'm wondering if you have an unneeded level of nesting. (I don't know if it
would make any difference, but it might).

INSERT INTO persona_followers(service_id, follower_id, valid_at)
SELECT DISTINCT service_id, follower_id, now()
FROM import
WHERE NOT EXISTS(SELECT * FROM persona_followers WHERE import.service_id =
persona_followers.service_id AND import.follower_id =
persona_followers.follower_id);

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Philipp Kraus 2013-04-17 14:44:17 Re: dataset lock
Previous Message Albe Laurenz 2013-04-17 14:15:36 Re: How large can a PostgreSQL database get?