如何在同一 postgresql 数据库的模式之间最有效地移动相似表(相同列数、数据类型。如果它们不相同,可以使用我希望的 View 来实现)中的数据?
编辑
抱歉含糊不清。我打算使用附加模式作为不经常需要的数据的存档(以提高性能)。更准确地说,超过 2 年的数据将被存档。服务器下线是可以的,但是不要超过一天,最多2天。是一个中型公司的会计软件。根据自由主义估计,一年的记录数量不会接近一百万。
最佳答案
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;
关于database - 在 postgresql 中同一数据库的模式之间有效地移动数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16069546/