On 12/01/2016 09:37 AM, Adrian wrote:
I would normally do session.query(Foo).count()

COUNT is somewhat expensive compared to just checking whether rows
exist, especially if lots of rows match (2.2M rows in the example):

I would have assumed you had some filtering condition.

Checking an EXISTS for an entire table at once, that may either have zero or 2.2M rows is a little unusual :) I'd do session.query(Foo.id).first() in that case.


In [2]: %timeit -n1 -r1 EventPerson.query.count()
1 loop, best of 1: 135 ms per loop
In [3]: %timeit -n1 -r1
db.session.query(EventPerson.query.exists()).scalar()
1 loop, best of 1: 2.44 ms per loop


how does this interact with filtering etc?
http://docs.sqlalchemy.org/en/latest/orm/query.html#sqlalchemy.orm.query.Query.exists
:P

It keeps all the filters present on the query object (in that example,
`self` is the existing query object)

well exists() returns a core SQL element whereas count() is special in that it invokes a query. You're possibly asking for some session.query.row_exists() type of thing.



this query is against column expressions, not entities, so eager
loading is not involved.
That's what I first thought, but the related OUTER JOINs are still
added. Maybe a bug?
https://gist.github.com/ThiefMaster/00f812a5be0ce321c91241de297dbdd0

it's not ideal it adds those in, there's the edge case that someone is joined eager loading with innerjoin=True and is expecting that to participate in the row being present, that's not the correct use of that but it could break things for someone.




--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and
Verifiable Example. See http://stackoverflow.com/help/mcve for a full
description.
---
You received this message because you are subscribed to the Google
Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to sqlalchemy+unsubscr...@googlegroups.com
<mailto:sqlalchemy+unsubscr...@googlegroups.com>.
To post to this group, send email to sqlalchemy@googlegroups.com
<mailto:sqlalchemy@googlegroups.com>.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

--
SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to