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

标签 database postgresql schema

如何在同一 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/

相关文章:

postgresql - 我应该如何修改 PostgreSQL 的 SQL 查询?

c++ - 从开箱即用的应用程序连接PostgreSQL的便捷方法? (嵌入PostgreSQL)

postgresql - 存储用户定义的段 JSONB 与单独的表?

java - 在Configuration中设置HDFS的根目录

database - HTML5数据库和localStorage是否可以跨子域共享?

postgresql - 无法更改postgres中列的数据类型

schema - 如何在 SPARX Enterprise Architect 中在包级别设置数据库模式?

mongodb - mongo 地理位置数据的 Meteor 简单模式

ruby-on-rails - rails 中数据修复的最佳替代方案?

php - 我们如何删除 $file_info[path] 末尾的斜杠 '/'