Re: Efficient Way to Merge Two Large Tables

From: Julian Mehnle <julian(at)mehnle(dot)net>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Efficient Way to Merge Two Large Tables
Date: 2010-07-13 23:08:28
Message-ID: 201007132308.29419.julian@mehnle.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Craig Ringer 2010-07-14 00:47:35 Re: Planner features, discussion
Previous Message Joshua Rubin 2010-07-13 22:54:22 Re: Efficient Way to Merge Two Large Tables