Re: MERGE: performance advices

Поиск
Список
Период
Сортировка
От Gregory Stark
Тема Re: MERGE: performance advices
Дата
Msg-id 87fxoiu4rm.fsf@oxford.xeocode.com
обсуждение исходный текст
Ответ на MERGE: performance advices  (Ivan Sergio Borgonovo <mail@webthatworks.it>)
Список pgsql-general
Ivan Sergio Borgonovo <mail@webthatworks.it> writes:

> I need to merge 2 tables:
>
> update d set c1=s.c1, c2=s.c2... from s where d.pk=s.pk;
> insert into d (pk, c1, c2, ...) select pk, c1, c2, c3 from s
>   where s.pk not in (select pk from d);

you could try making the not in an exists. In released versions of Postgres
sometimes one is better than the other. Raising work_mem might matter if it
lets you do a hash join for either the IN/EXISTS or the join.

There is another approach though whether it's faster depends on how many
indexes you have and other factors:

CREATE TABLE new_d AS
SELECT DISTINCT ON (pk) pk,c1,c
  FROM (select 1 as t, * from s
        union all
        select 2 as t, * from d
       )
 ORDER BY pk, t

This will pull in all the rows from both tables and sort them by pk with
records from s appearing before matching records from t and then keep only the
first value for each pk.

Then you'll have to build indexes, swap the tables, and fix any views or rules
which refer to the old table (they'll still refer to the old table, not the
new table even after renaming it to the old name).

--
  Gregory Stark
  EnterpriseDB          http://www.enterprisedb.com
  Get trained by Bruce Momjian - ask me about EnterpriseDB's PostgreSQL training!

В списке pgsql-general по дате отправления:

Предыдущее
От: ProAce
Дата:
Сообщение: question for upgrade pg 8.2.5 to pg 8.3.3
Следующее
От: "Richard Broersma"
Дата:
Сообщение: Re: MERGE: performance advices