diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2014-08-28 12:25:21 -0400 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2014-08-28 12:25:21 -0400 |
| commit | 685a014c644477a7e7cdb6aad4436d4422167209 (patch) | |
| tree | 269adc73f4d1615167e8c6f8737561cfe32df6d7 /lib/sqlalchemy | |
| parent | 00862a29c6c1494f1b55c3f93e5300f69fb4ac98 (diff) | |
| download | sqlalchemy-685a014c644477a7e7cdb6aad4436d4422167209.tar.gz | |
- A new implementation for :class:`.KeyedTuple` used by the
:class:`.Query` object offers dramatic speed improvements when
fetching large numbers of column-oriented rows.
fixes #3176
Diffstat (limited to 'lib/sqlalchemy')
| -rw-r--r-- | lib/sqlalchemy/orm/loading.py | 10 | ||||
| -rw-r--r-- | lib/sqlalchemy/orm/query.py | 5 | ||||
| -rw-r--r-- | lib/sqlalchemy/util/__init__.py | 2 | ||||
| -rw-r--r-- | lib/sqlalchemy/util/_collections.py | 70 |
4 files changed, 66 insertions, 21 deletions
diff --git a/lib/sqlalchemy/orm/loading.py b/lib/sqlalchemy/orm/loading.py index 232eb89de..934967b27 100644 --- a/lib/sqlalchemy/orm/loading.py +++ b/lib/sqlalchemy/orm/loading.py @@ -54,6 +54,9 @@ def instances(query, cursor, context): for query_entity in query._entities ])) + if not custom_rows and not single_entity: + keyed_tuple = util.lightweight_named_tuple('result', labels) + while True: context.progress = {} context.partials = {} @@ -72,8 +75,8 @@ def instances(query, cursor, context): elif single_entity: rows = [process[0](row, None) for row in fetch] else: - rows = [util.KeyedTuple([proc(row, None) for proc in process], - labels) for row in fetch] + rows = [keyed_tuple([proc(row, None) for proc in process]) + for row in fetch] if filtered: rows = util.unique_list(rows, filter_fn) @@ -126,6 +129,7 @@ def merge_result(querylib, query, iterator, load=True): if isinstance(e, querylib._MapperEntity)] result = [] keys = [ent._label_name for ent in query._entities] + keyed_tuple = util.lightweight_named_tuple('result', keys) for row in iterator: newrow = list(row) for i in mapped_entities: @@ -134,7 +138,7 @@ def merge_result(querylib, query, iterator, load=True): attributes.instance_state(newrow[i]), attributes.instance_dict(newrow[i]), load=load, _recursive={}) - result.append(util.KeyedTuple(newrow, keys)) + result.append(keyed_tuple(newrow)) return iter(result) finally: diff --git a/lib/sqlalchemy/orm/query.py b/lib/sqlalchemy/orm/query.py index 12e11b26c..15e0aa881 100644 --- a/lib/sqlalchemy/orm/query.py +++ b/lib/sqlalchemy/orm/query.py @@ -3275,9 +3275,10 @@ class Bundle(object): :ref:`bundles` - includes an example of subclassing. """ + keyed_tuple = util.lightweight_named_tuple('result', labels) + def proc(row, result): - return util.KeyedTuple( - [proc(row, None) for proc in procs], labels) + return keyed_tuple([proc(row, None) for proc in procs]) return proc diff --git a/lib/sqlalchemy/util/__init__.py b/lib/sqlalchemy/util/__init__.py index 15b2ac38e..d882c2656 100644 --- a/lib/sqlalchemy/util/__init__.py +++ b/lib/sqlalchemy/util/__init__.py @@ -21,7 +21,7 @@ from ._collections import KeyedTuple, ImmutableContainer, immutabledict, \ UniqueAppender, PopulateDict, EMPTY_SET, to_list, to_set, \ to_column_set, update_copy, flatten_iterator, \ LRUCache, ScopedRegistry, ThreadLocalRegistry, WeakSequence, \ - coerce_generator_arg + coerce_generator_arg, lightweight_named_tuple from .langhelpers import iterate_attributes, class_hierarchy, \ portable_instancemethod, unbound_method_to_callable, \ diff --git a/lib/sqlalchemy/util/_collections.py b/lib/sqlalchemy/util/_collections.py index 0904d454e..a1fbc0fa0 100644 --- a/lib/sqlalchemy/util/_collections.py +++ b/lib/sqlalchemy/util/_collections.py @@ -17,7 +17,20 @@ import types EMPTY_SET = frozenset() -class KeyedTuple(tuple): +class AbstractKeyedTuple(tuple): + def keys(self): + """Return a list of string key names for this :class:`.KeyedTuple`. + + .. seealso:: + + :attr:`.KeyedTuple._fields` + + """ + + return list(self._fields) + + +class KeyedTuple(AbstractKeyedTuple): """``tuple`` subclass that adds labeled names. E.g.:: @@ -56,23 +69,13 @@ class KeyedTuple(tuple): def __new__(cls, vals, labels=None): t = tuple.__new__(cls, vals) - t._labels = [] if labels: t.__dict__.update(zip(labels, vals)) - t._labels = labels + else: + labels = [] + t.__dict__['_labels'] = labels return t - def keys(self): - """Return a list of string key names for this :class:`.KeyedTuple`. - - .. seealso:: - - :attr:`.KeyedTuple._fields` - - """ - - return [l for l in self._labels if l is not None] - @property def _fields(self): """Return a tuple of string key names for this :class:`.KeyedTuple`. @@ -86,7 +89,10 @@ class KeyedTuple(tuple): :meth:`.KeyedTuple.keys` """ - return tuple(self.keys()) + return tuple([l for l in self._labels if l is not None]) + + def __setattr__(self, key, value): + raise AttributeError("Can't set attribute: %s" % key) def _asdict(self): """Return the contents of this :class:`.KeyedTuple` as a dictionary. @@ -100,6 +106,40 @@ class KeyedTuple(tuple): return dict((key, self.__dict__[key]) for key in self.keys()) +class _LW(AbstractKeyedTuple): + __slots__ = () + + def __new__(cls, vals): + return tuple.__new__(cls, vals) + + def __reduce__(self): + # for pickling, degrade down to the regular + # KeyedTuple, thus avoiding anonymous class pickling + # difficulties + return KeyedTuple, (list(self), self._real_fields) + + def _asdict(self): + """Return the contents of this :class:`.KeyedTuple` as a dictionary.""" + + d = dict(zip(self._real_fields, self)) + d.pop(None, None) + return d + + +def lightweight_named_tuple(name, fields): + + tp_cls = type(name, (_LW,), {}) + for idx, field in enumerate(fields): + if field is None: + continue + setattr(tp_cls, field, property(operator.itemgetter(idx))) + + tp_cls._real_fields = fields + tp_cls._fields = tuple([f for f in fields if f is not None]) + + return tp_cls + + class ImmutableContainer(object): def _immutable(self, *arg, **kw): raise TypeError("%s object is immutable" % self.__class__.__name__) |
