On 12/01/2016 11:49 AM, Adrian wrote:
> `from_self().exists()` seems to produce an unnecessarily complex query
> (still containing all the original columns)
it is unnecessarily complex in some situations but in others it is not.
count() for many years tried to guess which situations it could do a
straight SELECT COUNT and in which it needed to wrap inside of a
subquery, which itself was an error-prone process. New edge cases
continued to be reported. Once I moved it to a simple from_self(), it
returns the right result in all cases, the complexity of the source code
went way down, and I got my time back from constantly having to fight
new edge cases with it.
It's a reasonable tradeoff that count() returns the right result in all
cases with simple source code, and that a more optimized query if
necessary is available through manual use of func.count(), which is also
documented.
Here's the ticket where the logic for count() was made to just use
from_self() in all cases:
https://bitbucket.org/zzzeek/sqlalchemy/issues/2093
Here is the diff where 60 lines of buggy, complicated code were replaced
with two that have never failed since:
https://bitbucket.org/zzzeek/sqlalchemy/commits/e7be5a5729cabc6133b4fc83c06f6c5277a7af19#Llib/sqlalchemy/orm/query.pyF2057
If I had a crew of people to help triage bug reports, review code, and
write very high quality tests, areas like these may have more
out-of-the-box optimizations. Short of having that, having code that is
straightforward and returns the right result without issue is the next
best thing.