Python sqlalchemy backref慢

Python sqlalchemy backref慢,python,sqlalchemy,Python,Sqlalchemy,你们有下列表格吗 nfiletable = Table( 'NFILE', base.metadata, Column('fileid', Integer, primary_key=True), Column('path', String(300)), Column('filename', String(50)), Column('filesize', Integer), schema='NATIVEFILES')#,autoload=True,a

你们有下列表格吗

nfiletable = Table(
    'NFILE', base.metadata,
    Column('fileid', Integer, primary_key=True),
    Column('path', String(300)),
    Column('filename', String(50)),
    Column('filesize', Integer),
    schema='NATIVEFILES')#,autoload=True,autoload_with=engine)

sheetnames_table=Table(
    'SHEETNAMES', base.metadata, schema='NATIVEFILES', 
    autoload=True, autoload_with=engine)


nfile_sheet_table=Table(
    'NFILE_SHEETNAME',base.metadata,
    Column('fileid', Integer, ForeignKey(nfiletable.c.fileid)), 
    Column('sheetid', Integer, ForeignKey(sheetnames_table.c.sheet_id)),
    schema='NATIVEFILES')
和制图员:

nfile_mapper=mapper(Nfile,nfiletable)

mapper(Sheet, sheetnames_table, properties={
    'files': relation(
        Nfile, secondary=nfile_sheet_table,
        primaryjoin=(sheetnames_table.c.sheet_id==nfile_sheet_table.c.sheetid),
        secondaryjoin=(nfile_sheet_table.c.fileid==nfiletable.c.fileid),
        foreign_keys=[nfile_sheet_table.c.sheetid,nfile_sheet_table.c.fileid],
        backref='sheets')
})
当我做以下事情时

upl = Session.query(Nfile).filter_by(fileid=k).one()
sheetdb=[]
for sheet in sheetstoadd:
     s = sheetcache[sheetname]
     sheetdb.append(s)
upl.sheets = sheetdb
Session.save(upl)
Session.flush()
upl.sheets=sheetdb
将永远使用。 sheetdb中每个图纸的所有文件似乎都是从db加载的。
如何防止出现这种情况?

如果NFile.sheets引用了大量集合,请在backref中添加“lazy='dynamic'”:

mapper(Sheet, sheetnames_table, properties={
    'files': relation(
        Nfile, secondary=nfile_sheet_table,
        backref=backref('sheets', lazy='dynamic'))
})

所有primaryjoin/secondaryjoin/foreign\u Key的内容也不需要,因为
nfile\u sheet\u表上有
ForeignKey
结构。

如果nfile.sheets引用了一个巨大的集合,请在backref上加上“lazy='dynamic'”:

mapper(Sheet, sheetnames_table, properties={
    'files': relation(
        Nfile, secondary=nfile_sheet_table,
        backref=backref('sheets', lazy='dynamic'))
})
由于
nfile\u sheet\u表
上有
ForeignKey
结构,因此也不需要所有primaryjoin/secondaryjoin/ForeignKey内容