在postgresql中有效地在同一数据库的模式之间移动数据

时间:2013-04-17 20:20:41

标签: database postgresql schema

如何在相同的postgresql数据库的模式之间最有效地移动类似表中的数据(相同数量的列,数据类型。如果它们不相同,可以通过我希望的视图实现)?

修改

抱歉模糊不清。我打算使用其他模式作为不经常需要的数据的归档(以提高性能)。更准确地说,存档超过2年的数据。将服务器脱机是可以的,但不超过一天,最多2天。它是一家中型公司的会计软件。根据自由主义估计,一年内的记录数量不会接近一百万。

1 个答案:

答案 0 :(得分:3)

insert into target_schema.table_one (col1, col2, col3)
select col1, col2, col3
from source_schema.other_table
where <some condition to select the data to be moved>;

如果你真的想“移动”数据(即从源表中删除行),你需要使用

如果表是外键的目标,那么在这种情况下你不能使用truncate,你需要使用

delete from source_schema.other_table
where <some condition to select the data to be moved>;

如果您愿意,可以将两个步骤合并为一个语句:

with deleted_data as (
   delete from source_schema.other_table
   where <some condition to select the data to be moved>;
   returning *
)
insert into target_schema.table_one (col1, col2, col3)
select col1, col2, col3
from deleted_data;