I'm making the following query using flask-sqlalchemy. It works fine both ways, but I'm wondering, as my table grows, if there will be any performance impact to ordering it one way or another:
Option 1:
things = Thing.query.filter(Thing.id != 5).order_by(Thing.id.desc()).limit(20).all()
Option 2 (order_by and filter are swapped):
things = Thing.query.order_by(Thing.id.desc()).filter(Thing.id != 5).limit(20).all()
Thank you!
While SQL grammar is rather strict about the order of clauses that form a statement, the ORM query builder in SQLAlchemy is generative. It allows adding criteria and options in a more relaxed manner, and before execution it is compiled to produce the final SQL. In this light your two queries result in identical SQL, and so have no performance difference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With