在我的应用程序服务器中,我想使用LIMIT
和对数据集进行分页OFFSET
,并另外将数据集的总数返回给用户。
而不是对数据库进行两次远程调用:
select count(1) as total_count from foo;
select c1 from foo;
Run Code Online (Sandbox Code Playgroud)
我认为在单个数据库调用中完成此操作会更明智:
select c1, count(1) over (partition by null) from foo;
Run Code Online (Sandbox Code Playgroud)
但是,与不使用窗口函数相比,添加此窗口函数会导致执行时间长一个数量级。
我觉得这很令人惊讶,因为类似的时间select count(1) from foo
只需要两倍的时间select c1 from foo
。然而,将其转换为窗口函数会导致性能下降。
此外,使用以下使用子查询的替代方案非常快:
select c1, (select count(1) from foo) as total_count from foo;
Run Code Online (Sandbox Code Playgroud)
我本来期望 postgresql 能够优化partition by null
我在 Oracle 中尝试过这一点,发现了类似的性能损失。
如何解释为什么这里会出现性能损失?对于核心 postgresql 开发人员来说,进行更改以优化这一点是否相对容易,甚至值得,例如通过将 PARTITION BY NULL 的窗口函数转换为子查询?
设置:
drop table foo;
create table foo (c1 int);
insert into foo
select i from …
Run Code Online (Sandbox Code Playgroud) postgresql count window-functions postgresql-performance postgresql-13