使用PostgreSQL 10.5。我正在尝试创建一个分页系统,用户可以在其中来回切换各种结果。
为了不使用OFFSET
,我id
在名为p
(prevId)的参数中从上一页的最后一行传递了。然后我选择id
高于p
参数中传递的数字的前三行。(如本文所述)
例如,如果id
上一页的最后一行是 5,我会选择前 3 行的 anid
大于 5:
SELECT
id,
firstname,
lastname
FROM
people
WHERE
firstname = 'John'
AND id > 5
ORDER BY
ID ASC
LIMIT
3;
Run Code Online (Sandbox Code Playgroud)
这很好用,而且时机也不错:
Limit (cost=0.00..3.37 rows=3 width=17) (actual time=0.046..0.117 rows=3 loops=1)
-> Seq Scan on people (cost=0.00..4494.15 rows=4000 width=17) (actual time=0.044..0.114 rows=3 loops=1)
Filter: ((id > 5) AND (firstname = 'John'::text))
Rows Removed by Filter: …
Run Code Online (Sandbox Code Playgroud) postgresql performance index paging postgresql-10 query-performance