我想按日期范围对包含 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/