我正在使用 Flask 和 SQLAlchemy 创建 API。在我进行分页查询时的 get 端点中,即使两个查询都带来相同的数据,它也比不分页时更快(比如快两倍)。
字符串查询是这样的:
SELECT (
SELECT count(something1.id) where condition1 and condition2 as anon_1,
SELECT count(something2.id) where condition3 and condition4 as anon_2, ...
但是,当我对查询应用分页(即 query.limit(20).offset(0))时,查询更改为:
SELECT anon_1.anon_2 as anon_1_anon_2, anon_1.anon_3 as anon_1_anon_3 ...
FROM (SELECT (
SELECT count(something1.id) where condition1 and condition2 as anon_1,
SELECT count(something2.id) where condition3 and condition4 as anon_2, ...
LIMIT ALL OFFSET %(param_1)s ...
即使我应用无和 0 的分页(即 query.limit(None).offset(0))也是如此。
有谁知道为什么会这样?