"""This series of tests illustrates different ways to UPDATE a large numberof rows in bulk."""fromsqlalchemyimportColumnfromsqlalchemyimportcreate_enginefromsqlalchemyimportIntegerfromsqlalchemyimportStringfromsqlalchemy.ext.declarativeimportdeclarative_basefromsqlalchemy.ormimportSessionfrom.importProfilerBase=declarative_base()engine=NoneclassCustomer(Base):__tablename__="customer"id=Column(Integer,primary_key=True)name=Column(String(255))description=Column(String(255))Profiler.init("bulk_updates",num=100000)@Profiler.setupdefsetup_database(dburl,echo,num):globalengineengine=create_engine(dburl,echo=echo)Base.metadata.drop_all(engine)Base.metadata.create_all(engine)s=Session(engine)forchunkinrange(0,num,10000):s.bulk_insert_mappings(Customer,[{"name":"customer name %d"%i,"description":"customer description %d"%i,}foriinrange(chunk,chunk+10000)],)s.commit()@Profiler.profiledeftest_orm_flush(n):"""UPDATE statements via the ORM flush process."""session=Session(bind=engine)forchunkinrange(0,n,1000):customers=(session.query(Customer).filter(Customer.id.between(chunk,chunk+1000)).all())forcustomerincustomers:customer.description+="updated"session.flush()session.commit()