diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2014-11-06 17:29:22 -0500 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2014-11-06 17:31:14 -0500 |
| commit | 0c19d765dce89970c0395f57f15eb5b0f09c2a29 (patch) | |
| tree | 3394d8e89a6b09ece49e6af685548c724645640a /examples | |
| parent | 590498bf844e7dcdcf41d3ac786b4cccbebd2d43 (diff) | |
| download | sqlalchemy-0c19d765dce89970c0395f57f15eb5b0f09c2a29.tar.gz | |
bulk_updates
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/performance/bulk_updates.py | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/examples/performance/bulk_updates.py b/examples/performance/bulk_updates.py new file mode 100644 index 000000000..9522e4bf5 --- /dev/null +++ b/examples/performance/bulk_updates.py @@ -0,0 +1,54 @@ +"""This series of tests illustrates different ways to UPDATE a large number +of rows in bulk. + + +""" +from . import Profiler + +from sqlalchemy.ext.declarative import declarative_base +from sqlalchemy import Column, Integer, String, create_engine, bindparam +from sqlalchemy.orm import Session + +Base = declarative_base() +engine = None + + +class Customer(Base): + __tablename__ = "customer" + id = Column(Integer, primary_key=True) + name = Column(String(255)) + description = Column(String(255)) + + +Profiler.init("bulk_updates", num=100000) + + +@Profiler.setup +def setup_database(dburl, echo, num): + global engine + engine = create_engine(dburl, echo=echo) + Base.metadata.drop_all(engine) + Base.metadata.create_all(engine) + + s = Session(engine) + for chunk in range(0, num, 10000): + s.bulk_insert_mappings(Customer, [ + { + 'name': 'customer name %d' % i, + 'description': 'customer description %d' % i + } for i in range(chunk, chunk + 10000) + ]) + s.commit() + + +@Profiler.profile +def test_orm_flush(n): + """UPDATE statements via the ORM flush process.""" + session = Session(bind=engine) + for chunk in range(0, n, 1000): + customers = session.query(Customer).\ + filter(Customer.id.between(chunk, chunk + 1000)).all() + for customer in customers: + customer.description += "updated" + session.flush() + session.commit() |
