Re: Efficient Way to Merge Two Large Tables

Поиск
Список
Период
Сортировка
От Julian Mehnle
Тема Re: Efficient Way to Merge Two Large Tables
Дата
Msg-id 201007132308.29419.julian@mehnle.net
обсуждение исходный текст
Ответ на Efficient Way to Merge Two Large Tables  (Joshua Rubin <jrubin@esoft.com>)
Ответы Re: Efficient Way to Merge Two Large Tables  (Joshua Rubin <jrubin@esoft.com>)
Список pgsql-general
Joshua Rubin wrote:

> I have two tables each with nearly 300M rows. There is a 1:1
> relationship between the two tables and they are almost always joined
> together in queries. The first table has many columns, the second has
> a foreign key to the primary key of the first table and one more
> column. It is expected that for every row in table1, there is a
> corresponding row in table2. We would like to just add the one column
> to the first table and drop the second table to allow us to index this
> extra column.
>
> This query would work after adding the column to the first table:
> UPDATE table1 SET new_column = table2.new_column FROM table2 WHERE
> table1.row_id = table2.row_id;
>
> However, this will take much too long, I have not successfully
> completed this on our staging server after running it for 3+ days.

Can you get the query plan (EXPLAIN) of the update query?  My guess is the
join cost scales superlinearly.

You might be able to chop this up into smaller UPDATEs by limiting the
rows to be updated in each round by the primary key.

E.g.:

  UPDATE table1 SET new_column = table2.new_column FROM table2
    WHERE
      table1.row_id = table2.row_id and
      table1.row_id >= 0e6 and table1.row_id < 1e6 and
      table2.row_id >= 0e6 and table2.row_id < 1e6;

for a moving row_id window.

This has helped me in the past with a similar scenario (where both tables
were partitioned by the PK, but it would presumably still work in the
unpartitioned case).

-Julian

Вложения

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

Предыдущее
От: Joshua Rubin
Дата:
Сообщение: Re: Efficient Way to Merge Two Large Tables
Следующее
От: Craig Ringer
Дата:
Сообщение: Re: Planner features, discussion