sqlalchemy中多对多关系表的最佳性能

时间:2018-02-02 19:29:51

标签: python postgresql sqlalchemy flask-sqlalchemy

我的数据库中有一个时间关键组件,我正在尝试尽快检索这些信息。我的问题是如何最好地实现它。 我正在使用Flask-SQLalchemy,我有一个多对多关系的帮助表。我虽然我应该索引这个表,但我认为因为主键总是被索引我只能添加一个这样的主键

category_selection = db.Table(
    'category_selection',
    db.Column('user_id', db.Integer, db.ForeignKey('user.id')),
    db.Column('category_id', db.String(1000), db.ForeignKey('SocArxivCategory.id')),
    # The combination of the two foreign keys should be unique, since no user can 
    # follow the same category twice.
    db.PrimaryKeyConstraint('user_id', 'category_id')
)

但是,我从不通过这个主键访问此表,所以这有什么意义吗?那么我何时应该使用index = True,何时将主键添加到这样的帮助器表中呢?

我的底层数据库是Postgresql,上面的帮助表用作

selection = db.relationship('Category',
                            secondary=category_selection,
                            lazy='dynamic')

0 个答案:

没有答案
相关问题