在我的金字塔应用程序中,我有models.py
:
db = scoped_session(sessionmaker())
Base = declarative_base()
followers = Table('followers',
Base.metadata,
Column('follower_id', Integer, ForeignKey('artist.id')),
Column('followed_id', Integer, ForeignKey('artist.id')))
class Artist(Base):
__tablename__ = 'artist'
id = Column(Integer, primary_key=True)
title = Column(Text)
followed = relationship('Artist',
secondary=followers,
primaryjoin=(followers.c.follower_id == id),
secondaryjoin=(followers.c.followed_id == id),
backref=backref('followers', lazy='dynamic'),
lazy='dynamic')
Index('my_index', Artist.id, unique=True, mysql_length=255)
我的artist
表有85,632行,followers
辅助表有420,749行。
目前,如果我尝试像这样检索某些followed
的{{1}}:
artist
查询大约需要30-40毫秒来检索行,我怎样才能改进模型以减少这段时间?
顺便说一下,我的db.query(Artist).first().folowed.all()
模型基于此tutorial。
答案 0 :(得分:0)
解决方案:
followers.follower_id
。