postgresql - 如何在 postgres 中对现有表进行分区?

标签 postgresql postgresql-9.3

我想按日期范围对包含 100 万行以上的表进行分区。这通常如何在不需要太多停机时间或冒丢失数据的风险的情况下完成?以下是我正在考虑的策略,但欢迎提出建议:

1.现有表为主,子表继承。随着时间的推移,将数据从主表移动到子表,但会有一段时间,一些数据在主表中,一些在子表中。

2.创建一个新的主表和子表。在子表的现有表中创建数据副本(因此数据将驻留在两个地方)。一旦子表具有最新数据,更改所有插入以指向新的主表并删除现有表。

最佳答案

首先你要问问自己,表分区是否真的有必要。浏览分区文件:

https://www.postgresql.org/docs/9.6/static/ddl-partitioning.html

记住这个对数据分区非常重要的信息(来自上面的链接)

The benefits will normally be worthwhile only when a table would otherwise be very large. The exact point at which a table will benefit from partitioning depends on the application, although a rule of thumb is that the size of the table should exceed the physical memory of the database server.

您可以使用此 SQL 检查表的大小

SELECT pg_size_pretty(pg_database_size(<table_name>))

如果您遇到性能问题,请尝试重新编制索引或重新评估您的索引。检查您的 postgres 日志以进行自动清理。

1m+ 行不需要分区。

关于postgresql - 如何在 postgres 中对现有表进行分区?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48563168/

相关文章:

sql - 查询在 PostgreSQL 中有效,但在 HSQL 中失败。我该如何解决?

Django 操作错误 : could not fork new process for connection

postgresql - Postgres 集群 : password authentication failed for user “postgres”

sql - PRIMARY KEY 实际上表示什么,我的表需要一个吗?

postgresql - 带时区的 Postgres 时间戳

javascript - 有没有可能将 postgreSQL 直接连接到 Javascript?

python - 使用Postgresql使Sqlalchemy在过滤器中使用日期

jquery - 验证今天的星期几

sql - 从 upsert 方法返回行

postgresql - 尝试将 csv 文件导入 postgresql 时出现 "extra data after last expected column"