summaryrefslogtreecommitdiff
path: root/examples/performance/bulk_updates.py
diff options
context:
space:
mode:
authorSławek Ehlert <slafs@op.pl>2015-01-27 22:04:38 +0100
committerSławek Ehlert <slafs@op.pl>2015-01-27 22:04:38 +0100
commit57b2bd5dcba6140b511c898c0f682234f13d5c51 (patch)
treea0899b2a35d27e177001b163054c3c9a8f7f1c06 /examples/performance/bulk_updates.py
parent6a1f16d09958e549502a0991890d64964c71b357 (diff)
parent8aaa8dd6bdfb85fa481efa3115b9080d935d344c (diff)
downloadsqlalchemy-pr/152.tar.gz
Merge branch 'master' into oracle-servicename-optionpr/152
Diffstat (limited to 'examples/performance/bulk_updates.py')
-rw-r--r--examples/performance/bulk_updates.py54
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()