本文介绍了如何在 slick + postgresql 上进行分页选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 postgresql 数据库中,使用 slick 3,最好的分页方式是什么?
In a postgresql database, with slick 3, what's the best way to have pagination?
- 获取所有行并使用 Scala 进行分页(似乎效率不高)?
- 带有限制和偏移量的静态查询?
- 还有其他方法吗?
推荐答案
您可以在 TableQuery
对象上使用 take
和 drop
方法.它们将在生成的 SQL 查询中转换为 limit
和 offset
:
You can use take
and drop
methods on TableQuery
objects. They will be translated to limit
and offset
in the resulting SQL query:
val users: TableQuery[UsersTable] = UsersTable.query
val firstPartOfUsers = users.drop(0).take(25).result
val secondPartOfUsers = users.drop(25).take(25).result
这两个操作将转换为以下 SQL 查询:
Those two actions will be translated to the following SQL queries:
select "name", "email", "id" from "users" limit 25 offset 0
select "name", "email", "id" from "users" limit 25 offset 25
这篇关于如何在 slick + postgresql 上进行分页选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!