python - Sqlalchemy two filters vs one and -
this straight forward question. better use, 2 filters
or 1 and_
? there difference?
session.query(test).filter(test.id == x).filter(test.test == y).all()
vs
session.query(test).filter(and_(test.id == x, test.test == y)).all()
they give me same result there difference speed or else?
both queries in question have same performance. can test inspecting resulting sql query.
from sqlalchemy import column, integer, string, and_ sqlalchemy.ext.declarative import declarative_base sqlalchemy.orm.query import query base = declarative_base() class test(base): __tablename__ = 'test' id = column(integer, primary_key=true) test = column(string(50)) print(query([test]).filter(test.id == 1).filter(test.test == 'foo')) # select test.id test_id, test.test test_test # test # test.id = :id_1 , test.test = :test_1 print(query([test]).filter(and_(test.id == 1, test.test == 'foo'))) # select test.id test_id, test.test test_test # test # test.id = :id_1 , test.test = :test_1
both queries produce same sql expression.
and_ commonly used when you're using sql expression query directly against tables, or when nesting multiple predicates within or_
.
Comments
Post a Comment