【问题标题】:How to partition postgres table using intermediate table如何使用中间表对 postgres 表进行分区
【发布时间】:2018-08-09 04:23:43
【问题描述】:

我正在使用 postgres 10 db。

我的客户表由以下列组成

custid (primary key),
name,
phonenumber,
email,
dateofbirth,
address,
city,
country,
status(boolean)
Join_Date(Date)

我在表中有数百万条记录。我想在 Join_Date 和中间表的帮助下根据不同的月份(2018 年 1 月一个分区,2018 年 2 月一个分区,..etc)对表进行分区。

我还想编写自动化脚本,以便在月底表必须创建上个月的另一个分区

【问题讨论】:

  • 那么你到底想做什么
  • 我想借助中间(临时)表对客户表进行分区
  • 如 2018 年 1 月一个分区,2018 年 2 月一个分区
  • 是物理分区还是逻辑分区?
  • 逻辑分区

标签: sql postgresql postgresql-10


【解决方案1】:

试试这个方法:

  1. 首先,在客户表中创建一个附加列 想要进行逻辑分区。
  2. 然后使用客户更新该列 和中间表
  3. 更新后截断您的表

您可以在每个月运行此脚本,这将为您提供逻辑分区。

update customer set partition_column=to_char(Join_Date, 'YYYY-MM')
join intermediate_table on intermediate_table.custid=customer.custid
and  intermediate_table.Join_Date=customer.Join_Date

truncate table intermediate_table

【讨论】:

  • 您能否详细说明您的答案,因为我对 postgres 很陌生
  • 这适用于所有数据库,而不是特别是 postgres
【解决方案2】:

Postgres 文档中有一个针对您的问题的示例。

PostgreSQL: Documentation: 10: 5.10. Table Partitioning

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-10
    • 2018-12-16
    • 2022-10-15
    • 2016-11-27
    • 1970-01-01
    • 2019-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多