我正在使用金字塔Web框架开发Web应用程序。我将sqlalchemy用于ORM和模型。我正在使用Postgres for DB。
这是我的查询,用于提取所有用户提交的应用程序的各种计数:
counts = dbsession.query(
func.count(Application.id).filter(Application.state == state1),
func.count(Application.id).filter(Application.state == state2),
func.count(Application.id).filter(Application.state == state3)).first()
我怀疑这是否会多次扫描数据库,这是不希望的。
这是生成的sql:
SELECT count(applications.id)
FILTER (WHERE applications.state = %(state_1)s) AS anon_1, count(applications.id)
FILTER (WHERE applications.state = %(state_2)s) AS anon_2, count(applications.id)
FILTER (WHERE applications.state = %(state_3)s) AS anon_3
FROM applications
LIMIT %(param_1)s
谢谢。