You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用SQLAlchemy快速查询Facture表所有numero_facture字段值

有更高效的SQLAlchemy原生实现方案,可以直接仅查询目标字段,不需要遍历完整模型对象:

实现方法

方法1:兼容全版本Flask-SQLAlchemy写法

# 仅查询numero_facture字段,直接提取得到目标列表
numero_facture_list = [row[0] for row in Facture.query.with_entities(Facture.numero_facture).all()]

方法2:SQLAlchemy 1.4+ 极简写法

无需手动处理元组,直接返回符合预期的数值列表:

numero_facture_list = db.session.execute(db.select(Facture.numero_facture)).scalars().all()

效率优势

上述两种方案生成的SQL语句仅查询numero_facture单个字段,不会拉取表中其他无关字段的数据,相比查询全量模型对象再遍历取值的方案,内存占用更低、查询速度更快,在表数据量较大时优势格外明显。

内容的提问来源于stack exchange,提问作者Aravinth Balakrishnan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 16:45:03