性能对复合sqlalchemy查询的顺序有影响吗? [英] Any performance impact to the order of a compound sqlalchemy query?

查看:60
本文介绍了性能对复合sqlalchemy查询的顺序有影响吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用flask-sqlalchemy进行以下查询.两种方法都可以正常工作,但是我想知道,随着表的增加,以一种或另一种方式订购它是否会对性能产生影响:

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:

选项1:

things = Thing.query.filter(Thing.id != 5).order_by(Thing.id.desc()).limit(20).all()

选项2(互换了order_by和filter):

Option 2 (order_by and filter are swapped):

things = Thing.query.order_by(Thing.id.desc()).filter(Thing.id != 5).limit(20).all()

谢谢!

推荐答案

尽管SQL语法对形成语句的子句的顺序非常严格,但是SQLAlchemy中的ORM查询生成器是可生成的.它允许以更轻松的方式添加条件和选项,并且在执行之前对其进行编译以生成最终的SQL.因此,您的两个查询将导致相同的SQL,因此没有性能差异.

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.

这篇关于性能对复合sqlalchemy查询的顺序有影响吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆