diff options
author | Alessandro Cucci <alessandro.cucci@gmail.com> | 2018-08-25 09:14:22 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2018-08-27 16:10:55 -0400 |
commit | cbd661e0cdfdba98663ae542c6af2863fc30ae09 (patch) | |
tree | 4e899485ca82168baf8ac20a604c358e2aab0db4 /lib | |
parent | c6427fe14090d2d06a4d7c4c398be1f6e6a771f1 (diff) | |
download | sqlalchemy-cbd661e0cdfdba98663ae542c6af2863fc30ae09.tar.gz |
Add option to sort into inserts/updates to bulk_save_objects
Added new flag :paramref:`.Session.bulk_save_objects.preserve_order` to the
:meth:`.Session.bulk_save_objects` method, which defaults to True. When set
to False, the given mappings will be grouped into inserts and updates per
each object type, to allow for greater opportunities to batch common
operations together. Pull request courtesy Alessandro Cucci.
Change-Id: I0d041f7696cf733655a74beeceee3fa80640efd7
Pull-request: https://bitbucket.org/zzzeek/sqlalchemy/pull-requests/6
Diffstat (limited to 'lib')
-rw-r--r-- | lib/sqlalchemy/orm/session.py | 20 |
1 files changed, 17 insertions, 3 deletions
diff --git a/lib/sqlalchemy/orm/session.py b/lib/sqlalchemy/orm/session.py index 9ac529aeb..2c7fd86d2 100644 --- a/lib/sqlalchemy/orm/session.py +++ b/lib/sqlalchemy/orm/session.py @@ -2380,7 +2380,8 @@ class Session(_SessionClassMethods): transaction.rollback(_capture_exception=True) def bulk_save_objects( - self, objects, return_defaults=False, update_changed_only=True): + self, objects, return_defaults=False, update_changed_only=True, + preserve_order=True): """Perform a bulk save of the given list of objects. The bulk save feature allows mapped objects to be used as the @@ -2443,6 +2444,13 @@ class Session(_SessionClassMethods): When False, all attributes present are rendered into the SET clause with the exception of primary key attributes. + :param preserve_order: when True, the order of inserts and updates + matches exactly the order in which the objects are given. When + False, common types of objects are grouped into inserts + and updates, to allow for more batching opportunities. + + .. versionadded:: 1.3 + .. seealso:: :ref:`bulk_operations` @@ -2452,9 +2460,15 @@ class Session(_SessionClassMethods): :meth:`.Session.bulk_update_mappings` """ + def key(state): + return (state.mapper, state.key is not None) + + obj_states = tuple(attributes.instance_state(obj) for obj in objects) + if not preserve_order: + obj_states = sorted(obj_states, key=key) + for (mapper, isupdate), states in itertools.groupby( - (attributes.instance_state(obj) for obj in objects), - lambda state: (state.mapper, state.key is not None) + obj_states, key ): self._bulk_save_mappings( mapper, states, isupdate, True, |