Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any performance impact to the order of a compound sqlalchemy query?

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!

like image 900
Glenn Avatar asked Sep 17 '25 07:09

Glenn


1 Answers

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.

like image 180
Ilja Everilä Avatar answered Sep 19 '25 03:09

Ilja Everilä