diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2020-05-25 22:36:44 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2020-05-28 14:38:56 -0400 |
commit | 77f1b7d236dba6b1c859bb428ef32d118ec372e6 (patch) | |
tree | 7fae8eaaf303d6ce02bd423abf216550001e2f7b /lib/sqlalchemy/ext/baked.py | |
parent | 366e88ea0e5c5417184c1dd4776cff752560631d (diff) | |
download | sqlalchemy-77f1b7d236dba6b1c859bb428ef32d118ec372e6.tar.gz |
callcount reductions and refinement for cached queries
This commit includes that we've removed the "_orm_query"
attribute from compile state as well as query context.
The attribute created reference cycles and also added
method call overhead. As part of this change,
the interface for ORMExecuteState changes a bit, as well
as the interface for the horizontal sharding extension
which now deprecates the "query_chooser" callable
in favor of "execute_chooser", which receives the contextual
object. This will also work more nicely when we implement
the new execution path for bulk updates and deletes.
Pre-merge execution options for statement, connection,
arguments all up front in Connection. that way they
can be passed to the before_execute / after_execute events,
and the ExecutionContext doesn't have to merge as second
time. Core execute is pretty close to 1.3 now.
baked wasn't using the new one()/first()/one_or_none() methods,
fixed that.
Convert non-buffered cursor strategy to be a stateless
singleton. inline all the paths by which the strategy
gets chosen, oracle and SQL Server dialects make use of the
already-invoked post_exec() hook to establish the alternate
strategies, and this is actually much nicer than it was before.
Add caching to mapper instance processor for getters.
Identified a reference cycle per query that was showing
up as a lot of gc cleanup, fixed that.
After all that, performance not budging much. Even
test_baked_query now runs with significantly fewer function
calls than 1.3, still 40% slower.
Basically something about the new patterns just makes
this slower and while I've walked a whole bunch of them
back, it hardly makes a dent. that said, the performance
issues are relatively small, in the 20-40% time increase
range, and the new caching feature
does provide for regular ORM and Core queries that
are cached, and they are faster than non-cached.
Change-Id: I7b0b0d8ca550c05f79e82f75cd8eff0bbfade053
Diffstat (limited to 'lib/sqlalchemy/ext/baked.py')
-rw-r--r-- | lib/sqlalchemy/ext/baked.py | 49 |
1 files changed, 12 insertions, 37 deletions
diff --git a/lib/sqlalchemy/ext/baked.py b/lib/sqlalchemy/ext/baked.py index 112e245f7..7ac556dcc 100644 --- a/lib/sqlalchemy/ext/baked.py +++ b/lib/sqlalchemy/ext/baked.py @@ -230,10 +230,6 @@ class BakedQuery(object): # invoked statement = query._statement_20(orm_results=True) - # the before_compile() event can create a new Query object - # before it makes the statement. - query = statement.compile_options._orm_query - # if the query is not safe to cache, we still do everything as though # we did cache it, since the receiver of _bake() assumes subqueryload # context was set up, etc. @@ -243,7 +239,7 @@ class BakedQuery(object): # used by the Connection, which in itself is more expensive to # generate than what BakedQuery was able to provide in 1.3 and prior - if query.compile_options._bake_ok: + if statement.compile_options._bake_ok: self._bakery[self._effective_key(session)] = ( query, statement, @@ -383,7 +379,7 @@ class Result(object): return str(self._as_query()) def __iter__(self): - return iter(self._iter()) + return self._iter().__iter__() def _iter(self): bq = self.bq @@ -397,12 +393,14 @@ class Result(object): if query is None: query, statement = bq._bake(self.session) - q = query.params(self._params) + if self._params: + q = query.params(self._params) + else: + q = query for fn in self._post_criteria: q = fn(q) params = q.load_options._params - q.load_options += {"_orm_query": q} execution_options = dict(q._execution_options) execution_options.update( { @@ -463,16 +461,15 @@ class Result(object): Equivalent to :meth:`_query.Query.first`. """ + bq = self.bq.with_criteria(lambda q: q.slice(0, 1)) - ret = list( + return ( bq.for_session(self.session) .params(self._params) ._using_post_criteria(self._post_criteria) + ._iter() + .first() ) - if len(ret) > 0: - return ret[0] - else: - return None def one(self): """Return exactly one result or raise an exception. @@ -480,19 +477,7 @@ class Result(object): Equivalent to :meth:`_query.Query.one`. """ - try: - ret = self.one_or_none() - except orm_exc.MultipleResultsFound as err: - util.raise_( - orm_exc.MultipleResultsFound( - "Multiple rows were found for one()" - ), - replace_context=err, - ) - else: - if ret is None: - raise orm_exc.NoResultFound("No row was found for one()") - return ret + return self._iter().one() def one_or_none(self): """Return one or zero results, or raise an exception for multiple @@ -503,17 +488,7 @@ class Result(object): .. versionadded:: 1.0.9 """ - ret = list(self) - - l = len(ret) - if l == 1: - return ret[0] - elif l == 0: - return None - else: - raise orm_exc.MultipleResultsFound( - "Multiple rows were found for one_or_none()" - ) + return self._iter().one_or_none() def all(self): """Return all rows. |