根据唯一记录将一列从 sql table 转移到另一列 postgres

Transfer one column from one sql table to another based on unique records postgres

我有两个 sql(postgres) table,我需要将一列从一个 table 插入到另一个。 请注意,每个 table 包含大约 1 亿条记录

例如我的 table 模式:

first_table:
id int, first_column int, second_column int, third_column;

second_table:
id int, fourth_column int;

注意两个 table 中的 id 列是主键。

我需要得到以下 table:

first_table:
id int, first_column int, second_column int, third_column int, fourth_column int;

简而言之,我需要根据 id(主键)列合并这两个 table。

我试过:

  1. 为 first_table 添加一个名为 fourth_column 的空列并更新它。

    UPDATE first_column AS f
           SET fourth_column = t.fourth_column
           FROM second_table AS t
           WHERE f.id = t.id;

这个方法可行,但是每个 sql tables 包含大约 1 亿条记录,并且这个解决方案需要很多时间(对我的程序来说是关键时间)。

  1. 使用某些类型的 postgres 连接,但文档中的示例让我失望。

是否存在某种方法或规则可以在短时间内完成update/transfer。也许我应该使用一些高级的大数据库,例如 SparkSQL 或其他东西。

此致, 哇

我假设您已经有一个成功填充 first_tablesecond_table 的系统。

如果是这种情况,则在需要将数据放在一起时连接两个表:

select f.id, f.first_column, f.second_column, f.third_column, s.fourth_column
  from first_table f
  join second_table s
    on s.id = f.id
 where f.first_column = 200
   and s.fourth_column = 110

如果表有 1 亿条记录,那么我假设您正在使用一些标准来限制返回的行,如我的示例中的 where 子句。