如何使用Peewee创建一个分区表?

huangapple go评论67阅读模式
英文:

How to create a partitioned table with Peewee?

问题

当使用Peewee定义数据模型时,如何按照给定字段进行分区?我想按照日期列进行分区,例如,按照每年的月份进行分区。

我在Peewee的文档中看到可以在数据模型的Meta部分添加自定义约束,那么如何添加分区命令,该命令将放在表创建DDL的末尾?

是否可以在架构创建后执行此操作?这也是一种方法...

英文:

When defining my data model with peewee, how can I make a table partitioned by a given field? I’m interested in partitioning a table by a date column, for example, by month of the year.

I see in Peewee’s documentation that I can add custom constraints as part of the Meta part of the data model, how can I then add then partitioning command which would go at the end of the table creation DDL?

Is there a way to do it post schema creation? This would also be a way to go …

答案1

得分: 2

我在我的废弃目录中找到了一些代码,这些代码可能会对你有所帮助 - 这里我有一个文档表,根据“is_reviewed”列对文档进行了分区:

db = PostgresqlDatabase('parttest')

class Base(Model):
    class Meta:
        database = db

class Document(Base):
    # 我认为这样做是必要的,而不是使用SERIAL。
    id = IntegerField(sequence='document_id_seq', index=True)
    name = CharField()
    is_reviewed = BooleanField()
    class Meta:                               
        table_settings = 'PARTITION BY LIST (is_reviewed)'                               

# 用于直接访问分区的文档子类。                                      
class DocumentReviewed(Document):
    class Meta:
        table_name = 'document_reviewed'

class DocumentUnreviewed(Document):
    class Meta:
        table_name = 'document_unreviewed'

db.create_tables([Document])

# 创建分区。
db.execute_sql('create table document_reviewed partition of document '                   
               'for values in (true)')
db.execute_sql('create table document_unreviewed partition of document '                 
               'for values in (false)')

希望这对你有帮助。

英文:

I found some code in my scrap directory that may help you - here I've got a document table that is partitioned into docs that are reviewed/un-reviewed (based on the "is_reviewed" column):

db = PostgresqlDatabase('parttest')

class Base(Model):
    class Meta:
        database = db

class Document(Base):
    # I believe it is necessary to do it this way vs using SERIAL.
    id = IntegerField(sequence='document_id_seq', index=True)
    name = CharField()
    is_reviewed = BooleanField()
    class Meta:                               
        table_settings = 'PARTITION BY LIST (is_reviewed)'                               


# Doc subclasses for accessing partitions directly.                                      
class DocumentReviewed(Document):
    class Meta:
        table_name = 'document_reviewed'

class DocumentUnreviewed(Document):
    class Meta:
        table_name = 'document_unreviewed'


db.create_tables([Document])

# Create the partitions.
db.execute_sql('create table document_reviewed partition of document '                   
               'for values in (true)')
db.execute_sql('create table document_unreviewed partition of document '                 
               'for values in (false)')

huangapple
  • 本文由 发表于 2023年3月31日 15:51:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75896095.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定