Dan*_*iro 6 postgresql select count
我的问题与此类似: Need a row count after SELECT statements: What's the best SQL method?
但我想从查询中获取行总数,然后使用 limit 来创建分页,这样我就无法使用返回的行。从一个简单的查询:
select * from res_groups
Run Code Online (Sandbox Code Playgroud)
我进入了这个:
select a.*, (select count(1) from (select * from res_groups) e) total
from (select * from res_groups) a limit 10 offset 10;
Run Code Online (Sandbox Code Playgroud)
或者我可以使用简单的方法并进行两个查询:
select * from res_groups limit 10;
select count(*) from res_groups;
Run Code Online (Sandbox Code Playgroud)
第一个查询是否具有性能?我担心来自 res_groups 的查询会被执行两次?
还有其他办法吗?ps:我使用的是postgres,我知道mysql有FOUND_ROWS()
关于什么:
WITH a AS (select *, count(*) over (range unbounded preceding)
FROM resgroups)
SELECT * from a order by foo limit 10 offset 10;
Run Code Online (Sandbox Code Playgroud)
现在,我认为您实际上最好将其分成两个查询,因为看起来您正在有效地进行分页。如果您首先选择 count(*),然后决定需要多少页(并且可能缓存该结果),那么后续的部分查询可以使用索引,但在这种情况下,每组 10 个将需要完整的顺序扫描。
| 归档时间: |
|
| 查看次数: |
7181 次 |
| 最近记录: |